wordpress 如何列出自定义帖子类型中的所有类别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39652122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to list all category from custom post type?
提问by ilovebali
I have a post type called 'dining' and has a taxonomy called 'dining-category'.
我有一个名为“dining”的帖子类型,还有一个名为“dining-category”的分类法。
What I want to do is, I want to display all the category from post type 'dining' in my footer area.
我想要做的是,我想在我的页脚区域显示帖子类型“餐饮”中的所有类别。
回答by deemi-D-nadeem
In WordPress 4.6 get_terms
is deprecated. So there is an alternate of this (get_categories
) Read this
在 WordPress 4.6get_terms
中已弃用。所以有一个替代这个 ( get_categories
)阅读这个
And here is Example code:
这是示例代码:
<?php
$args = array(
'taxonomy' => 'dining-category',
'orderby' => 'name',
'order' => 'ASC'
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
<a href="<?php echo get_category_link( $cat->term_id ) ?>">
<?php echo $cat->name; ?>
</a>
<?php
}
?>
Hope this will help you.
希望这会帮助你。
回答by Husain Ahmed
<?php
$args = array(
'type' => 'dining',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'dining-category',
'pad_counts' => false );
$categories = get_categories($args);
echo '<ul>';
foreach ($categories as $category) {
$url = get_term_link($category);?>
<li><a href="<?php echo $url;?>"><?php echo $category->name; ?>cc</a></li>
<?php
}
echo '</ul>';
?>
If category not assigned any post it will not show. therefore assign any post. This code running perfectly.
如果类别未分配任何帖子,则不会显示。因此分配任何职位。这段代码完美运行。
回答by Purnendu Sarkar
<?php
$wcatTerms = get_terms(
'category', array('hide_empty' => 0, 'number' => 3, 'order' =>'asc', 'parent' =>0));
foreach($wcatTerms as $wcatTerm) :
?>
<small><a href="<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>"><?php echo $wcatTerm->name; ?></a></small>
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $wcatTerm->slug,
)
),
'posts_per_page' => 1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$imgurl = get_the_post_thumbnail_url( get_the_ID(), 'full' );
$title=get_the_title($post->ID);
?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endwhile; wp_reset_postdata(); ?>
<?php endforeach; ?>
回答by Raman Kumar
use get_terms()
function to fetch custom taxonomy by its slug, in your case slug is dining-category.
read function refrence from wordpress codex website and try this.
使用get_terms()
函数通过其 slug 获取自定义分类法,在您的情况下,slug 是餐饮类别。从 wordpress codex 网站读取函数引用并尝试这个。