dso89762
2018-11-28 14:44
浏览 58
已采纳

WordPress自定义帖子类型显示在特定位置

I'm new to WordPress so I have a little problem with Custom Post Types. I have created a new Custom Post Type like this:

function awesome_custom_post_type(){
    $labels = array(
        'name' => 'Top Categorii',
        'singular-name' => 'Top Categorie',
        'add_new' => 'Adauga Categorie',
        'all_items' => 'Toate Categoriile',
        'add_new_item' => 'Adauga Categorie',
        'edit_item' => 'Editeaza Categorie',
        'new_item' => 'Categorie Noua',
        'view_item' => 'Vezi Categorie',
        'search_item' => 'Cauta Categorie',
        'not_found' => 'Categoria nu a fost gasita',
        'not_found_in_trash' => 'Categoria nu a fost gasita in cos',
        'Parent_item_colon' => 'Categorie parinte',
    );
    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'publicly_queryable' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'thumbnail',
            'revisions',
        ),
        'taxonomies' => array('category', 'post_tag'),
        'menu_position' => 10,
        'exclude_from_search' => true
    );
    register_post_type('top_categorii', $args);
}
add_action('init', 'awesome_custom_post_type');

Everything works fine, but I want to display the posts of this type in a specific location, to be more precise, in page.php template above the normal posts, so how can I do this? Is it possible?

Another question on this subject, if I click on a custom post I want it to get me to the page where are displayed some normal posts of a specific category. (like the page that shows up when you click on a category). Is this possible too?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drrog9853 2018-11-28 15:32
    已采纳

    I am using this code to display CPT(Custom Post Types) on specific page/s,

    <?php
    $query = new WP_Query(array(
    'post_type' => 'slug', // Put here your Custom Post Type Slug/s
    'posts_per_page' => -1, // -1 for displaying all the Posts
    'order' => ASC //ASC for Ascending Order,and USE DESC for Descending Order
    ));
    if ( $query->have_posts() ) : while ($query->have_posts()) : $query->the_post();
    ?>
    
    <h3><?php the_title() ;?></h3>
    <p>
    <?php the_content(); ?>
    </p>
    <?php endwhile;?>
    <?php endif; ?>
    <?php wp_reset_postdata();?> 
    

    Hope this Would Help you! :)

    打赏 评论

相关推荐 更多相似问题