2016-03-27 54 views
1

我使用Barcelona theme我需要獲取某個類別中發佈的作者。獲得作者誰在類別中的帖子

在我author.php模板,我有:

$barcelona_authors = get_users(array(
    'fields' => 'ID', 
    'who' => 'authors', 
    'order' => 'DESC', 
    'orderby'=> 'post_count' 
)); 

<?php 
foreach ($barcelona_authors as $barcelona_author_id) { 
    barcelona_author_box($barcelona_author_id, false); 
} 
?> 

如何讓誰已經張貼到類別編號59的作者?

例如,我試着用:

$barcelona_authors = get_posts('category=59'); 

但我發現了錯誤。任何幫助?

錯誤:

Notice: Object of class WP_Post could not be converted to int in /home/wp-includes/author-template.php on line 296

+1

有什麼錯誤? – Technoh

回答

0

這對Barcelona theme解決方案:

$barcelona_posts = get_posts('cat=59'); 
$barcelona_author_ids = array(); 

foreach ($barcelona_posts as $k => $v) { 
    if (! in_array($v->post_author, $barcelona_author_ids)) { 
     $barcelona_author_ids[] = $v->post_author; 
    } 
} 

$barcelona_authors = get_users(array(
    'fields' => 'ID', 
    'who' => 'authors', 
    'order' => 'DESC', 
    'orderby'=> 'post_count', 
    'include' => $barcelona_author_ids 
)); 
0

get_posts('category=59')代碼應工作。我在我的一個測試wordpress安裝中測試了以下內容,並且它可以工作(當然有不同的類別ID)。如果您在撥打get_posts時遇到錯誤,您需要向我們顯示錯誤。

<?php 

    $category_posts = get_posts('category=59'); 
    $authors_ids = array(); 
    $authors = array(); 

    foreach($category_posts as $cat_post) { 
     $authors_ids[] = $cat_post->post_author; 
    } 

    // Not sure if you need more data than just the ID so here is what you need 
    // to get other fields. 

    foreach($authors_ids as $id) { 
     $user = get_user_data($id); 
     // Display name: $user->display_name; 
     // Nice name: $user->user_nicename; 
     // etc... 
    } 

?> 
相關問題