我编写了一个函数,每个页面只显示有限数量的最新帖子,而且效果很好。但我也希望每个帖子只显示一个类别名称(即使该帖子有几个其他类别),我只希望它显示每个帖子的第一个[0]类别。
我花了几个小时在这上面,我已经非常接近了,但最后的帖子显示了列表上的所有类别,就像使用<?php the_category(" "); ?>
我非常熟悉下面的代码,它只显示每个帖子的第一个类别名称,但我不能让它也回显到该类别的永久链接,只有文本字符串:-/
<?php
$query = new WP_Query(array(
\'posts_per_page\' => 2,
\'categories_per_page\' => 1
));
while ($query->have_posts()): $query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<!-- Gets category name but not the link -->
<?php $category = get_the_category();
echo $category[0]->cat_name; ?>
<?php endwhile; ?>
我已经阅读了关于以下主题的所有内容:https://codex.wordpress.org/Function_Reference/get_category_linkhttps://developer.wordpress.org/reference/functions/wp_list_categories/我曾尝试将其包装在永久链接中,但不断出现错误。有人能帮我吗?:-)