微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在博客页面帖子页面上显示类别名称

如何解决如何在博客页面帖子页面上显示类别名称

大家好,我希望您一切都好,我在如何在“帖子”页面或“博客页面显示类别名称时遇到了麻烦,有人可以给我一个关于如何显示类别名称的想法。

这是它的外观(红色矩形是类别名称):

enter image description here

<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/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。