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

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

报告相同问题?

悬赏问题

  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据