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

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! :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题