无法在WordPress上打印带标题的类别

时间:2014-02-14 09:31:53

标签: php wordpress

我正在尝试打印类似

的内容
  

[category]帖子标题

使用此代码:

$args = array( 'numberposts' => '10' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
        $category = get_the_category($recent["ID"]);
        echo '<li>['. $category["name"] .'] <a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
        var_dump($category);
    }

但是我收到了这个错误

  

注意:尝试在....中获取非对象的属性

这是当前的输出

[] Olá, mundo!
array(1) { [0]=> object(stdClass)#2068 (17) { ["term_id"]=> &int(3) ["name"]=> &string(5) "PT-PT" ["slug"]=> &string(5) "pt-pt" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(3) ["taxonomy"]=> string(8) "category" ["description"]=> &string(0) "" ["parent"]=> &int(0) ["count"]=> &int(1) ["object_id"]=> int(1) ["filter"]=> string(3) "raw" ["cat_ID"]=> &int(3) ["category_count"]=> &int(1) ["category_description"]=> &string(0) "" ["cat_name"]=> &string(5) "PT-PT" ["category_nicename"]=> &string(5) "pt-pt" ["category_parent"]=> &int(0) } }

谢谢你们!

1 个答案:

答案 0 :(得分:2)

$args = array( 'numberposts' => '10' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
        $category = get_the_category($recent["ID"]);
        echo '<li>['. $category[0]->name .'] <a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
        var_dump($category);
    }