WordPress获取自定义帖子类型的分类列表 [英] wordpress get taxonomy list of custom post type

查看:165
本文介绍了WordPress获取自定义帖子类型的分类列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的wordpress网站上使用视频主题。在此主题中,定义了视频发布类型和视频类别分类。这是分类法的注册代码:

I am using "Video" theme for my wordpress website. In this theme, videos post type and "videoscategory" taxonomy are defined. Here is the register code for taxonomy:

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 
 )
            );

}

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

I added some categories under videoscategory taxonomy. Now i want to get all the categories that belongs to videoscategory. I googled the whole day, and I use

$tax_terms =get_terms('videoscategory');

但获取空数组。有谁知道这是什么问题?谢谢。

but get empty array. Does anyone know what is the problem? thanks.

推荐答案

在这里使用此代码,您肯定会找到解决方案

Here is use this code, you definitely find your solution

$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

这篇关于WordPress获取自定义帖子类型的分类列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆