WordPress 获取自定义帖子类型的分类列表

2024-03-28

我正在为我的 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');

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


这是使用这段代码,你一定能找到你的解决方案

$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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WordPress 获取自定义帖子类型的分类列表 的相关文章

随机推荐