2017-07-03 56 views
0

我想顯示給定作者使用的所有類別,其中父級和子級顯示在作者檔案中具有層次結構。顯示作者在Wordpress作者檔案中使用的類別

實施例:

  • A1是A
  • A2的孩子的
  • 子提交張貼1個帖子在A1
  • 提交張貼1個帖子在A2
  • 作者在C和A1中發佈了1篇文章(相同文章)
  • 作者在B中發佈了1篇文章

結果想:

<ul> 
<li><a href="http://example.com/?cat=56">A</a> 
    <ul> 
     <li><a href="http://example.com/?cat=57">A1</a></li> 
     <li><a href="http://example.com/?cat=58">A2</a></li> 
    </ul> 
</li> 
<li><a href="http://example.com/?cat=3">B</a></li> 
<li><a href="http://example.com/?cat=4">C</a></li> 
<li><a href="http://example.com/?cat=5">D is not displayed because author didn't post in this category</a></li> 
</ul> 

回答

0
$cat_array = array(); 
$args=array(
'author' => get_the_author_meta('id'), 
'showposts'=>-1, 
'caller_get_posts'=>1 
); 
$author_posts = get_posts($args); 
if($author_posts) { 
    foreach ($author_posts as $author_post) { 
    foreach(get_the_category($author_post->ID) as $category) { 
     $cat_array[$category->term_id] = $category->term_id; 
    } 
    } 
} 

$cat_ids = implode(',', $cat_array); 
echo $output = strtr(wp_list_categories('include='.$cat_ids.'&title_li=&style=none&echo=0'), array('<br />' => '/'));