2013-04-08 57 views
0

我正在一個網站上有自定義作者頁,並在作者頁上有一個最近的作者帖子小部件,顯示該作者的其他職位。這個網站有多個作者,所以每篇文章都有所不同,我還沒有找到這樣的插件。我試圖顯示帖子縮略圖,標題和類別名稱。獲取環境外的the_category

我正在使用顯示標題和縮略圖的函數,但它沒有該類別。我嘗試添加類別:the_category('','multiple',$ authors_post-> ID),不幸的是它顯示了第一個li中的所有類別。而不是每個帖子。

這裏是我的工作:

function get_related_author_posts() { 
global $authordata, $post; 

$authors_posts = get_posts(array('author' => $authordata->ID, 
    'post__not_in' => array($post->ID), 'posts_per_page' => 3)); 

$output = '<ul>'; 
foreach ($authors_posts as $authors_post) { 
    $output .= '<li>' . get_the_post_thumbnail($authors_post->ID, 'xsmall-thumb') 
     . '<p>' . the_category(' ','multiple',$authors_post->ID) 
     . '</p> <a href="' . get_permalink($authors_post->ID) 
     . '">' . apply_filters('the_title', $authors_post->post_title, 
     $authors_post->ID) . '</a></li>'; 
} 
$output .= '</ul>'; 

return $output; 

}

任何幫助將不勝感激, 謝謝!

回答

0

請試試這個代碼,

<?php 
$cat=1; 
$yourcat = get_category($cat); 
if ($yourcat) 
{ 
    echo '<h2>' . $yourcat->name . '</h2>'; 
} 
?> 

要獲取類別名稱,

+0

謝謝,但這似乎並沒有工作:( – user1272433 2013-04-08 05:42:42

+0

它應該與'get_the_category($ cat_id)'... – 2016-05-02 09:58:50

0

嘗試像

function get_related_author_posts() { 
global $authordata, $post; 

$authors_posts = get_posts(array('author' => $authordata->ID, 'post__not_in' => array($post->ID), 'posts_per_page' => 3)); 

$output = '<ul>'; 
foreach ($authors_posts as $authors_post) { 
$category = get_the_category($authors_post->ID); 
foreach($category as $c){ 
$cat=$c->cat_name.' '.$cat; 
} 

    $output .= '<li>' . get_the_post_thumbnail($authors_post->ID, 'xsmall-thumb') . '<p>' . $cat . '</p> <a href="' . get_permalink($authors_post->ID) . '">' . apply_filters('the_title', $authors_post->post_title, $authors_post->ID) . '</a></li>'; 
} 
$output .= '</ul>'; 

return $output; 
} 

希望一些事情這項工作你..

+0

謝謝!這讓我更接近我的目標,它顯示類別名稱,不,我只需要它輸出鏈接到類別。 – user1272433 2013-04-08 14:37:05