问题描述
大家好,我希望您一切都好,我在如何在“帖子”页面或“博客”页面上显示类别名称时遇到了麻烦,有人可以给我一个关于如何显示类别名称的想法。
这是它的外观(红色矩形是类别名称):
<div class="common_wrapper">
<div class="wrapper_100">
<?PHP
$current_month = null;
while ( have_posts() ) : the_post();
$this_year = get_the_date( 'Y' );
if ( $this_year !== $current_year ) {
echo '<h2>' . $this_year . '</h2>';
}
$current_year = $this_year;?>
<a href="<?PHP echo get_permalink(); ?>">
<?PHP the_date('Y/m/d')?>
<?PHP the_content()?>
</a>
<?PHP endwhile;?>
</div>
</div>
谢谢
解决方法
您必须使用get_terms()
函数来获取该特定分类法的所有类别。
$terms = get_terms( 'my_taxonomy' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
my_taxonomy
是您的分类法或类别名称。
参考:https://developer.wordpress.org/reference/functions/get_terms/