我正在尝试获取一个属于两个不同类别的帖子的类别。一个类别始终是“特色”类别,而其他类别则会发生变化。
我只能看到这个词Array()
这是我的代码:
<?php $args = array( \'numberposts\' => \'1\', \'order\' => \'DESC\',\'category\' => 17 );
$recent_posts = wp_get_recent_posts( $args );
//Now lets do something with these posts
foreach( $recent_posts as $recent )
{
echo \'<div id="testata">\';
echo \'<a href="\' . get_permalink($recent["ID"]) . \'" title="Dettaglio \'.esc_attr($recent["post_title"]).\'" >\' ;
echo get_the_post_thumbnail($recent["ID"],"thumb-category") ;
echo \'</a>\';
echo \'<div class="testata_inner">\';
echo \'<span class="\'.get_the_category($recent["cat_name"]) . \'">\';
if ($cat[0]->name != "Featured") {
echo $cat[0]->name;
} else {
echo $cat[1]->name;
}
echo \'</span>\';
echo \'<p>\'.esc_attr($recent["post_title"]).\'</p>\';
echo \'<span class="\' . esc_attr($recent["post_author"]) . \'">\' . esc_attr($recent["post_author"]) . \'</span>\';
echo \'</div> \';
echo \'</div> \';
} ?>
最合适的回答,由SO网友:epilektric 整理而成
这应该会让你朝着正确的方向前进。
echo \'<div class="testata_inner">\';
$cat = get_the_category($recent["ID"]); //get the category array
if ($cat[0]->name != "Featured") { //check if the category is "featured"
$catname = $cat[0]->name; //set $catname variable to "featured"
} else {
$catname = $cat[1]->name; //or set $catname variable to "other category name"
}
echo \'<span class="\'. $catname . \'">\';
echo $catname; //output $catname variable
echo \'</span>\';