如何在WordPress中显示所有可用的自定义帖子类别?
问题描述:
如何在主屏幕上显示自定义帖子类型的所有类别,而不列出项目。如何在WordPress中显示所有可用的自定义帖子类别?
我已经创建了自定义帖子类型和它的类别,现在我需要在我的主页上显示所有类别作为链接到每个类别页面。有人可以帮忙吗?
答
$args = array('post_type' => 'post', 'posts_per_page' => 10);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
+0
这将列出自定义posttype的项目,而不是它的类别。 –
答
$cat_args = array(
'taxonomy' => 'your-custom-post', // your custom post type
);
$custom_terms = get_categories($cat_args);
echo print_r($custom_terms);
+1
这是错误的。分类参数预计不存在帖子类型。看到这里 - > [get_categories](https://developer.wordpress.org/reference/functions/get_categories/) –
答
可以使用现在get_categories
下面是代码的例子:
<?php
$args = array(
'taxonomy' => 'Your Taxonomy Name'
'hide_empty' => 0,
'orderby' => 'name'
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
<a href="<?php echo get_category_link($cat->slug); ?>">
<?php echo $cat->name; ?>
</a>
<?php
}
?>
记住写你的分类名称你注册,在这里'Your Taxonomy Name'
例如product_cat
,blog_cat
等
希望这会帮助你。
答
<?php
$terms = get_terms('taxonamy_name', array(
'orderby' => 'count',
'hide_empty' => 0
));
foreach($terms as $term)
{
echo $term->name;
}?>
</ul>
</div>
对[此帖的第二个答案]看看(http://wordpress.stackexchange.com/questions/14331/get-terms-by-taxonomy-and-post-type#answer-14334) 。 –