Php while循环中的回显类别名称

Php while循环中的回显类别名称,php,wordpress,Php,Wordpress,我正在while循环中显示我的自定义帖子类型“projects”。但是所有的帖子都属于不同的类别,所以我想显示每个项目的类别名称,但它不会输出类别名称 以下是循环: if ( $query->have_posts() ) { while ($query->have_posts()) { $query->the_post(); ?> <

我正在while循环中显示我的自定义帖子类型“projects”。但是所有的帖子都属于不同的类别,所以我想显示每个项目的类别名称,但它不会输出类别名称

以下是循环:

if ( $query->have_posts() ) {
                    while ($query->have_posts()) {
                        $query->the_post(); ?>
                        <?php if( has_post_thumbnail() ) { ?>
                            <div>
                                <img src="<?php echo the_post_thumbnail_url('home-project-thumb'); ?>" style="width: 100%; height: auto; max-height: 625px;">
                                <div style="background-image:url('<?php echo get_template_directory_uri(); ?>/assets/images/projectslider-<?php echo $color; ?>.svg'); background-repeat: no-repeat; background-size: cover;" class="projectslider-item">
                                    <div class="projectslider-content"><?php $categories = get_the_category(); $cat_name = $categories[0]->cat_name; ?>
                                        <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
                                        <p <?php if ( is_page(95) ) { ?> style="color: white!important;" <?php } ?>><?php the_excerpt(); ?></p><br>
                                        <a href="<?php the_permalink(); ?>" class="projectslider-button projectslider-button-purple">Lees meer</a>
                                    </div>
                                </div>
                            </div>
                <?php } ?> <?php } } wp_reset_postdata(); ?>
<?php $categories = get_the_category(); $cat_name = $categories[0]->cat_name; ?>
我在循环中添加了以下代码:

if ( $query->have_posts() ) {
                    while ($query->have_posts()) {
                        $query->the_post(); ?>
                        <?php if( has_post_thumbnail() ) { ?>
                            <div>
                                <img src="<?php echo the_post_thumbnail_url('home-project-thumb'); ?>" style="width: 100%; height: auto; max-height: 625px;">
                                <div style="background-image:url('<?php echo get_template_directory_uri(); ?>/assets/images/projectslider-<?php echo $color; ?>.svg'); background-repeat: no-repeat; background-size: cover;" class="projectslider-item">
                                    <div class="projectslider-content"><?php $categories = get_the_category(); $cat_name = $categories[0]->cat_name; ?>
                                        <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
                                        <p <?php if ( is_page(95) ) { ?> style="color: white!important;" <?php } ?>><?php the_excerpt(); ?></p><br>
                                        <a href="<?php the_permalink(); ?>" class="projectslider-button projectslider-button-purple">Lees meer</a>
                                    </div>
                                </div>
                            </div>
                <?php } ?> <?php } } wp_reset_postdata(); ?>
<?php $categories = get_the_category(); $cat_name = $categories[0]->cat_name; ?>

您似乎只是简单地将名称分配给变量,需要将其作为输出进行“回显”:

<?php

$categories = get_the_category();

$cat_name = $categories[0]->cat_name;

echo $cat_name;

?>

您似乎只是简单地将名称分配给变量,需要将其作为输出进行“回显”:

<?php

$categories = get_the_category();

$cat_name = $categories[0]->cat_name;

echo $cat_name;

?>

如果您使用的是自定义分类法,则需要获取以下术语:

其中category是自定义分类法的slug名称


PS:如果您使用的是默认的WordPress类别,则获取术语的工作方式与上述示例类似。

如果您使用的是自定义分类法,则需要获取术语:

其中category是自定义分类法的slug名称


PS:如果您使用默认的WordPress类别,获取\u\u术语的工作方式与上述示例类似。

您的cpt是否支持默认的wp分类类别?如果您有自定义的帖子类型,则获取帖子类别:您的cpt是否支持默认的wp分类类别?如果您有自定义的帖子类型,则获取帖子类别:获取\u该帖子类别仅返回默认类别的结果分类学对于自定义分类法,请使用get_the_terms.get_the_category仅返回默认分类法的结果。对于自定义分类法,请使用获取术语。