【问题标题】:wordpress get taxonomy list of custom post typewordpress 获取自定义帖子类型的分类列表
【发布时间】:2014-10-23 04:50:34
【问题描述】:

我正在为我的 wordpress 网站使用“视频”主题。在这个主题中,定义了视频帖子类型和“视频类别”分类。这是分类学的注册码:

add_action("init", "custom_posttype_menu_wp_admin1");
function custom_posttype_menu_wp_admin1()
{

 register_post_type('videos', 
            array('label' => __('Videos','templatic'),
                    'labels' => array(  'name' => __('Video','templatic'),
                                'singular_name' => __('Video','templatic'),
                                'add_new' => __('Add Video','templatic'),
                                'add_new_item'=> __('Add New Video','templatic'),
                                'edit' => __('Edit','templatic'),
                                'edit_item'=> __('Edit Video','templatic'),
                                'new_item' => __('New Video','templatic'),
                                'view_item' => __('View Video','templatic'),
                                'search_items' => __('Search Videos','templatic'),
                                'not_found' => __('No Videos found','templatic'),
                                'not_found_in_trash'=> __('No Videos found in trash','templatic')   
                               ),
                    'public'            => true,
                    'can_export'        => true,
                    'show_ui'           => true, // UI in admin panel
                    '_builtin'          => false, // It's a custom post type, not built in
                    '_edit_link'        => 'post.php?post=%d',
                    'capability_type'   => 'post',
                    'menu_icon'         => get_bloginfo('template_url').'/images/favicon.ico',
                    'hierarchical'      => false,
                    'rewrite'           => array("slug" => "videos"), // Permalinks
                    'query_var'         => "videos", // This goes to the WP_Query schema
                    'supports'          => array(   'title',
                                                    'author', 
                                                    'excerpt',
                                                    'thumbnail',
                                                    'comments',
                                                    'editor', 
                                                    'trackbacks',
                                                    'custom-fields',
                                                    'revisions') ,
                    'show_in_nav_menus' => true ,
                    'taxonomies'        => array('videoscategory','videostags')
                )
            );
// Register custom taxonomy
register_taxonomy(  "videoscategory", 
            array(  "videos"    ), 
            array ( "hierarchical"=> true, 
                    "label"=> "Category", 
                    'labels'=> array(   'name' => __('Category','templatic'),
                             'singular_name'=> __('Category','templatic'),
                             'search_items'=> __('Search Category','templatic'),
                             'popular_items' => __('Popular Category','templatic'),
                              'all_items'=> __('All Category','templatic'),
                              'parent_item' => __('Parent Category','templatic'),
                              'parent_item_colon' => __('Parent Category:','templatic'),
                              'edit_item' => __('Edit Category','templatic'),
                              'update_item'=> __('Update Category','templatic'),
                              'add_new_item'=> __('Add New Category','templatic'),
                              'new_item_name'=> __('New Make Category','templatic') ), 
                              'public'=> true,
                              'show_ui' => true,
                              'query_var'=> true,
                              "rewrite" => true 
 )
            );

}

我在视频类别分类下添加了一些类别。现在我想获取属于视频类别的所有类别。我用谷歌搜索了一整天,我使用

$tax_terms =get_terms('videoscategory');

但得到空数组。有谁知道是什么问题?谢谢。

【问题讨论】:

  • 使用以下方式轻松获取所有自定义帖子类型类别:wp.me/p4esuX-3k

标签: wordpress custom-post-type custom-taxonomy


【解决方案1】:

这里是使用这个代码,你肯定会找到你的解决方案

$cat_args = array(
    'orderby'       => 'term_id', 
    'order'         => 'ASC',
    'hide_empty'    => true, 
);

$terms = get_terms('videoscategory', $cat_args);

    foreach($terms as $taxonomy){
         $term_slug = $taxonomy->slug;

    $tax_post_args = array(
          'post_type' => 'videos',
          'posts_per_page' => 999,
          'order' => 'ASC',
          'tax_query' => array(
                array(
                     'taxonomy' => 'videoscategory',
                     'field' => 'slug',
                     'terms' => '$term_slug'
                )
           )
    );

    $tax_post_qry = new WP_Query($tax_post_args);

    if($tax_post_qry->have_posts()) :
         while($tax_post_qry->have_posts()) :
                $tax_post_qry->the_post();

                the_title();

          endwhile;

    else :
          echo "No posts";
    endif;
} //end foreach loop

【讨论】:

    【解决方案2】:

    当您访问get_terms页面时,codex中有一个示例

    $terms = get_terms('videoscategory');
     if ( !empty( $terms ) && !is_wp_error( $terms ) ){
         echo "<ul>";
         foreach ( $terms as $term ) {
           echo "<li>" . $term->name . "</li>";
    
         }
         echo "</ul>";
     }
    

    提示:在自定义分类下创建的对象称为术语,而不是类别。请看this post我已经在WPSE上完成了

    【讨论】:

      【解决方案3】:

      这是我用于常见问题解答帖子类型的示例:

      get_header();
      if(have_posts()) {
          $args = array (
              'orderby'    => 'name',
              'order'      => 'ASC',
              'hide_empty' => true,
          );
          $terms = get_terms( 'faq_levels', $args );
          foreach ( $terms as $term ) {
              echo $term->slug;
      
              $post_args = array (
                  'post_type'  => 'faq',
                  'faq_levels' => $term->name,
              );
              $query = new WP_Query( $post_args );
              while ( $query->have_posts() ) {
                  $query->the_post(); ?>
      

      我创建了这样的帖子类型:

      function post_type_faqs() {
          register_post_type('faq', array(
              'label'=>'FAQ',
              'menu_icon' => '',
              'labels'=>array(
                  'name'=>_x('FAQs', 'post type general name'),
                  'singular_name'=>_x('FAQ', 'post type singular name'),
                  'add_new'=>_x('Add New', 'faq'),
                  'add_new_item'=>__('Add New FAQ'),
                  'edit_item'=>__('Edit FAQ'),
                  'new_item'=>__('New FAQ'),
                  'view_item'=>__('View FAQ'),
                  'search_items'=>__('Search FAQs'),
                  'not_found'=>__('No faqs found'),
                  'not_found_in_trash'=>__('No faqs found in Trash'),
                  'parent_item_colon'=>''),
              'public'=>true,
              'publicly_queryable'=>true,
              'show_ui'=>true,
              'query_var'=>true,
              'rewrite'=>false,
              'capability_type'=>'post',
              'supports'=>array('title','thumbnail','post-formats'),
              'taxonomies'=>array('post_tag','faq_category'),
              'slug'=>'faq',
              'hierarchical'=>false,
              'menu_position'=>6,
              'show_in_nav_menus'=> true,
              'has_archive' => true,
              'can_export' => true,
              'register_meta_box_cb' => 'faq_add_meta_boxes'
      
          ));
      }
      add_action('init', 'post_type_faqs');
      
      /**
       * Create Subjects taxonomy
       */
      function faq_category() {
          $labels = array(
              'name'                       => _x( 'Categories', 'Taxonomy General Name', 'dc' ),
              'singular_name'              => _x( 'Category', 'Taxonomy Singular Name', 'dc' ),
              'menu_name'                  => __( 'Categories', 'dc' ),
              'all_items'                  => __( 'All Categories', 'dc' ),
              'parent_item'                => __( 'Parent Category', 'dc' ),
              'parent_item_colon'          => __( 'Parent Category:', 'dc' ),
              'new_item_name'              => __( 'New Category', 'dc' ),
              'add_new_item'               => __( 'Add New Category', 'dc' ),
              'edit_item'                  => __( 'Edit Category', 'dc' ),
              'update_item'                => __( 'Update Category', 'dc' ),
              'separate_items_with_commas' => __( 'Separate categories with commas', 'dc' ),
              'search_items'               => __( 'Search Categories', 'dc' ),
              'add_or_remove_items'        => __( 'Add or remove categories', 'dc' ),
              'choose_from_most_used'      => __( 'Choose from the most used categories', 'dc' ),
              'not_found'                  => __( 'Not Found', 'dc' ),
          );
          $args = array(
              'labels'                     => $labels,
              'hierarchical'               => true,
              'public'                     => true,
              'show_ui'                    => true,
              'show_admin_column'          => true,
              'show_in_nav_menus'          => true,
              'show_tagcloud'              => true,
              'sort_column'                => 'menu_order',
      
          );
          register_taxonomy( 'faq_category', array( 'faq' ), $args );
      }
      add_action( 'init', 'faq_category', 0 );
      

      【讨论】:

        猜你喜欢
        • 2016-10-28
        • 1970-01-01
        • 1970-01-01
        • 2013-06-22
        • 1970-01-01
        • 2011-06-26
        • 2017-06-02
        • 1970-01-01
        • 2013-08-03
        相关资源
        最近更新 更多