显示属于两个不同类别的帖子的类别名称

时间:2014-10-23 作者:apetto

我正在尝试获取一个属于两个不同类别的帖子的类别。一个类别始终是“特色”类别,而其他类别则会发生变化。

我只能看到这个词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> \';

    } ?>

1 个回复
最合适的回答,由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>\';

结束

相关推荐

Show Pages in Categories

通过将此代码添加到函数中,我创建了category函数。php:function page_category() { register_taxonomy_for_object_type(\'category\', \'page\'); } // Add to the admin_init hook of your theme functions.php file add_action( \'init\', \'page_category\' ); 但问