2015-06-28 59 views
0

我使用此代碼顯示所有標題由catgory id在短代碼函數中使用。get_post標題wordpress顯示評論框

global $post; 
$args = array('category' => 8); 
$myposts = get_posts($args); 

foreach($myposts as $post){ 
    $post_title=$post_title."<br>".$post->post_title; 
} 

$post_data.=$post_title; 
echo $post_data; 

問題是此代碼顯示最後一篇文章和評論框的評論。

問題是由foreach生成的,我刪除了foreach並正常工作。

我不知道我該怎麼做才能防止這種情況發生。

當我只使用這個代碼工作正常,但我需要所有的標題。

$title_id= get_the_title(214); 

在頁面我只是把[name_shortcode]

回答

0

要附加新價值$post_data但那是外循環。你應該把它放在循環內以追加所有的值。

$post_data =''; 
foreach($myposts as $post){ 
    $post_title=$post_title."<br>".$post->post_title; 
    $post_data.=$post_title; 
}