在我的主页上,我试图循环播放一定数量的帖子并显示每个帖子的类别名称。我试图显示名称的类别是父类的子类别(如果这有任何区别)。这是我的代码:
<?php
$args = array( 'numberposts' => '15' );
$recent_posts = wp_get_recent_posts( $args );
foreach ( $recent_posts as $recent ) {
$category = get_the_category($recent['ID']);
echo $category['cat-name'];
}; ?>
当我跑步时,没有任何结果显示出来。该页面显示好像代码不在那里。但是当我在$ category上执行var_dump时:
<?php
$args = array( 'numberposts' => '15' );
$recent_posts = wp_get_recent_posts( $args );
foreach ( $recent_posts as $recent ) {
$category = get_the_category($recent['ID']);
echo var_dump($category);
}; ?>
我可以看到我需要的一切都在那里。每个帖子,包含它的键和值,但出于某种原因试图显示帖子的特定属性(例如“猫名”,甚至“名称&#39;”)不起作用。有什么想法吗?
答案 0 :(得分:0)
答案 1 :(得分:0)
试试这个
<?php get_cat_name( $cat_id ) ?>
示例
<?php echo get_cat_name(4);?>
如果您想了解更多关于类别名称显示或其他关于wordpress类别的信息,可以访问https://codex.wordpress.org/Function_Reference/get_cat_name