Php wp_查询中的WordPress单个类别名称

Php wp_查询中的WordPress单个类别名称,php,wordpress,foreach,categories,Php,Wordpress,Foreach,Categories,使用下面的wp_查询从三个不同类别中获取最新帖子。作为这项工作的一部分,我想显示适用于每一篇文章的类别名称,该名称为_类别;但是,它会在名称周围放置一个和标记。我只想知道类别的名称 <?php $categories = get_categories(); foreach ($categories as $category) { } $args = array( 'cat' => 'article,fitness,recipe', 'posts_per_page'

使用下面的wp_查询从三个不同类别中获取最新帖子。作为这项工作的一部分,我想显示适用于每一篇文章的类别名称,该名称为_类别;但是,它会在名称周围放置一个和标记。我只想知道类别的名称

<?php
$categories = get_categories();
foreach ($categories as $category)
{

}
$args = array(
    'cat' => 'article,fitness,recipe',
    'posts_per_page' => '3',
);

$query = new WP_Query($args);

if ($query->have_posts())
{

    ?>
    <?php
    while ($query->have_posts())
    {
        $query->the_post();
        ?>
        <article id="post-<?php the_ID(); ?>" class="col">
            <h5><?php echo the_category(); ?></h5>
        </article>

    <?php } // end while  ?> 
<?php
} // end if
wp_reset_postdata();
?>
您可以使用获取链接到的类别 那个帖子

替换这个

<article id="post-<?php the_ID(); ?>" class="col">
    <h5><?php echo the_category(); ?></h5>
</article>
用这个

<article id="post-<?php the_ID(); ?>" class="col">
    <?php
    $category_detail = get_the_category(get_the_ID());
    $cat_arr = [];
    foreach ($category_detail as $cd)
    {
        $cat_arr[] = $cd->cat_name;
    }
    ?>
    <h5><?= !empty($cat_arr) ? implode(', ', $cat_arr) : 'N/A'; ?></h5>
</article>
仅供参考:_类别回显由其自身包含,因此您无需回显


希望这有帮助

太好了!谢谢劳纳克。将此答案标记为正确,也可以随意对问题进行投票!