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

时间:2020-11-12 12:17:19

标签: php wordpress wordpress-theming categories custom-wordpress-pages

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

这是它的外观(红色矩形是类别名称):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> 

谢谢

1 个答案:

答案 0 :(得分:0)

您必须使用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/