2013-01-13 34 views
-1

如何列出類別的作者?Wordpress列出該類別的作者

例如:

CAT NAME: INTERNET  AUTHOR: JOHN, DOE, ALEX 
CAT NAME: TECH   AUTHOR: JOHN 
CAT NAME: CODE   AUTHOR: ALEX 

任何想法如何做到這一點的WordPress的?

回答

2

你可以使用如下代碼:

<?php 
$cat_arr = get_categories(); // Get the list of Categories 
foreach ($cat_arr as $cat_obj) { 
    $term_id = $cat_obj->term_id; 

    // Print the Name 
    ?> 
    <br> 
    CAT NAME: <?php echo $cat_obj->name ?>, AUTHOR: 
    <?php 
    // Get all Posts of that Category 
    $posts = get_posts(array('category'=>$term_id)); 

    $authors_arr = array(); 
    foreach ($posts as $post_obj) { 
     $author_id = $post_obj->post_author; 

      // In depends on where you put this code, the include of the file is required 
     if (!function_exists('get_userdata')) { 
      include '<your WP folder>/wp-includes/pluggable.php'; 
     } 

     $user_obj = get_userdata($author_id); 
      // Only Add the author is isn't already added, to avoid printed twice 
     if (!in_array($user_obj->user_login, $authors_arr)) { 
      $authors_arr[] = $user_obj->user_login; // Instead of user_login you can use any Database field of the "Users" table 
     } 
    } 
    echo implode(', ', $authors_arr) . '<br>'; 
} 
?> 

但是,如果你想爲「當前」類別做到這一點(不是所有種類),你可以使用如下代碼:

$cat_obj = get_the_category(); // This is what you put in your comment to get Current Category 

    $term_id = $cat_obj->term_id; 

    // Print the Name 
    ?> 
    <br> 
    CAT NAME: <?php echo $cat_obj->name ?>, AUTHOR: 
    <?php 
    // Get all Posts of that Category 
    $posts = get_posts(array('category'=>$term_id)); 

    $authors_arr = array(); 
    foreach ($posts as $post_obj) { 
     $author_id = $post_obj->post_author; 

      // In depends on where you put this code, the include of the file is required 
     if (!function_exists('get_userdata')) { 
      include '<your WP folder>/wp-includes/pluggable.php'; 
     } 

     $user_obj = get_userdata($author_id); 
      // Only Add the author is isn't already added, to avoid printed twice 
     if (!in_array($user_obj->user_login, $authors_arr)) { 
      $authors_arr[] = $user_obj->user_login; // Instead of user_login you can use any Database field of the "Users" table 
     } 
    } 
    echo implode(', ', $authors_arr) . '<br>'; 
+0

大作品!非常感謝@ gsc-leticia – Genxer

+0

你會看這張圖嗎? [鏈接](http://www.screencast.com/t/tb3aQdZY) 我該怎麼做?我知道我不知道Php編碼很好。 – Genxer

+0

最後,你的代碼工作完美,我只需要鏈接作者姓名。我該怎麼做?我用wordpress函數嘗試它,但它沒有用你的代碼<?php the_author_link(); ?>你能再幫助我嗎? :) @ gsc-leticia Regards – Genxer

相關問題