2012-08-13 63 views
1

目前我在做下面讓所有作者的列表:WordPress的 - 如何獲得所有的作者(或所有用戶)名稱及其相關的ID?

$authors = wp_list_authors('html=0&style=none&echo=0&exclude_admin=1&optioncount=0&show_fullname=1&hide_empty=1&orderby=name&order=ASC'); 

$authors_array = explode(',', $authors); 

for ($j = 0; $j < count($authors_array); $j++) 
{ 
    echo '<li id="">'.$authors_array[$j].'</li>'; 
} 

我怎樣才能也得到了用戶的ID?

我到處尋找,我想不出一種方法來獲取所有作者和另一個元數據。

回答

1

您可以重寫functions.php中的函數,也可以根據原始函數創建一個新函數。查看代碼很簡單,只需在foreach循環中包含$ author_id即可。

Code for original is here

試試這個,確保你添加新的參數(includeauthorid)插入函數調用...

$authors = wp_list_authors('html=0&style=none&echo=0&exclude_admin=1&optioncount=0&show_fullname=1&hide_empty=1&orderby=name&order=ASC&includeauthorid=1'); 

新功能 - PS我不會建議改變原。只需把它放在functions.php中即可。

function wp_list_authors($args = '') { 
global $wpdb; 

$defaults = array(
    'orderby' => 'name', 'order' => 'ASC', 'number' => '', 
    'optioncount' => false, 'exclude_admin' => true, 
    'show_fullname' => false, 'hide_empty' => true, 
    'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true, 
    'style' => 'list', 'html' => true, 
    'includeauthorid' => false 
); 

$args = wp_parse_args($args, $defaults); 
extract($args, EXTR_SKIP); 

$return = ''; 

$query_args = wp_array_slice_assoc($args, array('orderby', 'order', 'number')); 
$query_args['fields'] = 'ids'; 
$authors = get_users($query_args); 

$author_count = array(); 
foreach ((array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql('post') . " GROUP BY post_author") as $row) 
    $author_count[$row->post_author] = $row->count; 

foreach ($authors as $author_id) { 
    $author = get_userdata($author_id); 

    if ($exclude_admin && 'admin' == $author->display_name) 
     continue; 

    $posts = isset($author_count[$author->ID]) ? $author_count[$author->ID] : 0; 

    if (!$posts && $hide_empty) 
     continue; 

    $link = ''; 

    if ($show_fullname && $author->first_name && $author->last_name) 
     $name = "$author->first_name $author->last_name"; 
    else 
     $name = $author->display_name; 

    if($includeauthorid) 
     $name .= ' ('. $author_id .')'; 

    if (!$html) { 
     $return .= $name . ', '; 

     continue; // No need to go further to process HTML. 
    } 

    if ('list' == $style) { 
     $return .= '<li>'; 
    } 

    $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr(sprintf(__("Posts by %s"), $author->display_name)) . '">' . $name . '</a>'; 

    if (!empty($feed_image) || !empty($feed)) { 
     $link .= ' '; 
     if (empty($feed_image)) { 
      $link .= '('; 
     } 

     $link .= '<a href="' . get_author_feed_link($author->ID) . '"'; 

     $alt = $title = ''; 
     if (!empty($feed)) { 
      $title = ' title="' . esc_attr($feed) . '"'; 
      $alt = ' alt="' . esc_attr($feed) . '"'; 
      $name = $feed; 
      $link .= $title; 
     } 

     $link .= '>'; 

     if (!empty($feed_image)) 
      $link .= '<img src="' . esc_url($feed_image) . '" style="border: none;"' . $alt . $title . ' />'; 
     else 
      $link .= $name; 

     $link .= '</a>'; 

     if (empty($feed_image)) 
      $link .= ')'; 
    } 

    if ($optioncount) 
     $link .= ' ('. $posts . ')'; 

    $return .= $link; 
    $return .= ('list' == $style) ? '</li>' : ', '; 
} 

$return = rtrim($return, ', '); 

if (!$echo) 
    return $return; 

echo $return; 

}

+0

感謝,這是一個很好的建議,但你有沒有關於如何編輯現有函數以包含id的任何想法? – martincarlin87 2012-08-13 12:54:56

+0

剛剛看到你的更新回答,我從來沒有嘗試過,因爲我最終找到了另一種我將發佈的方式,但是感謝你去幫助! – martincarlin87 2012-08-13 14:16:09

+0

沒問題。這是好的,你有一個好的翻找,因爲它總是有助於下次你卡住;) – Stokedout 2012-08-14 12:19:30

2

我發現另一種方法是做到以下幾點(發誓,我再也找不到像這樣的早期東西):

$authors = get_users('role=author&orderby=display_name&order=ASC'); 

foreach ($authors as $author) { 
    if (count_user_posts($author->ID) > 0) { 
     echo '<li id="' . $author->ID . '">' . $author->display_name . '</li>'; 
    } 
} 
相關問題