display the posts of Custom Post type in custom category

I create custom post type named "project".and the custom taxonomy of project is named "project_cat".

I create custom post type named "project".

and the custom taxonomy of project is named "project_cat".

so my question is that if I create an "apple" taxonomy,and the URL will be http://localhost/project_cat/apple.

how do I display the posts that belong "apple" taxonomy?

// Custom post type project
    function ateam_project_post_types(){

        $labels = array(
          'name'              => '全部的專案',
          'add_new'           => '新增任務',
          'add_new_item'      => '新增任務',
          'edit_item'         => '編輯專案',
          'all_items'         => '全部任務',
          'view_item'         => '查看專案',
          'search_items'      => '搜尋任務',
          'not_found'         => '沒有相關專案',
          'not_found_in_trash'=> '垃圾桶裡並沒有相關專案',
          'menu_name'         => '專案管理系統'
        );

        $args = array(
          'supports'          => array('title','editor'),
          'rewrite'           => array('slug' => 'project'),
          'public'            => true,
          'has_archive'       => true,
          'hierarchical'      => false,
          'menu_icon'         => 'dashicons-text-page',
          'menu_position'     => 1,
          'labels'            => $labels
        );

        register_post_type('project',$args);

      }
      add_action('init','ateam_project_post_types');


    // custom taxonomy of project
    function reg_cat() {

        $labels = array(
          'search_items' =>'搜尋專案',
          'all_items'    =>'所有專案',
          'add_new_item' =>'新增專案',
          'menu_name'    =>'全部專案'
        );

        $args = array(
          'labels'      => $labels,
          'hierarchical'=> true,
          'show_admin_column' => true,
          'rewrite'     => array('slug' => 'project_cat')
        );

        register_taxonomy( 'project_cat',array('project'),$args);

      }
      add_action('init', 'reg_cat');

I create custom post type named "project".

and the custom taxonomy of project is named "project_cat".

so my question is that if I create an "apple" taxonomy,and the URL will be http://localhost/project_cat/apple.

how do I display the posts that belong "apple" taxonomy?

// Custom post type project
    function ateam_project_post_types(){

        $labels = array(
          'name'              => '全部的專案',
          'add_new'           => '新增任務',
          'add_new_item'      => '新增任務',
          'edit_item'         => '編輯專案',
          'all_items'         => '全部任務',
          'view_item'         => '查看專案',
          'search_items'      => '搜尋任務',
          'not_found'         => '沒有相關專案',
          'not_found_in_trash'=> '垃圾桶裡並沒有相關專案',
          'menu_name'         => '專案管理系統'
        );

        $args = array(
          'supports'          => array('title','editor'),
          'rewrite'           => array('slug' => 'project'),
          'public'            => true,
          'has_archive'       => true,
          'hierarchical'      => false,
          'menu_icon'         => 'dashicons-text-page',
          'menu_position'     => 1,
          'labels'            => $labels
        );

        register_post_type('project',$args);

      }
      add_action('init','ateam_project_post_types');


    // custom taxonomy of project
    function reg_cat() {

        $labels = array(
          'search_items' =>'搜尋專案',
          'all_items'    =>'所有專案',
          'add_new_item' =>'新增專案',
          'menu_name'    =>'全部專案'
        );

        $args = array(
          'labels'      => $labels,
          'hierarchical'=> true,
          'show_admin_column' => true,
          'rewrite'     => array('slug' => 'project_cat')
        );

        register_taxonomy( 'project_cat',array('project'),$args);

      }
      add_action('init', 'reg_cat');
Share Improve this question asked Jul 17, 2019 at 15:09 cindycindy 1059 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I'm glad you've asked this, because doing this wrong is by far the most common mistake on this site. It's not even close to whatever #2 is.

The answer is that you don't need to do anything. WordPress will query the correct posts for you based on the URL. You just need to use the normal loop and template tags to display them:

<?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
        <?php the_title(); ?>
        <!-- etc. -->
    <?php endwhile; ?>
<?php endif; ?>

That loop is what loops through the posts that WordPress has already correctly queried based on the URL. So there's no need for any custom WP_Query, get_posts() or pre_get_posts action. Nothing like that.

The exact template that will be used depends on what files are in your theme, and are determined by the template hierarchy, but all those templates should only include the exact same loop as above. The only reason you'd need to create a taxonomy-project_cat.php template is if you wanted this taxonomy's archives to look different or use different markup, and if you did want that then you still use that same loop, because the query is always taken care of already.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745314998a4622176.html

相关推荐

  • display the posts of Custom Post type in custom category

    I create custom post type named "project".and the custom taxonomy of project is named "project_cat".

    17小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信