2017-10-05 36 views
0

我有一個短代碼應該返回單個頁面上多個自定義帖子的標題和內容。它正確顯示每篇文章的標題,但是當顯示每篇文章的內容時,它只會顯示第一篇文章的內容。我究竟做錯了什麼?弄清楚如何從簡碼中獲取內容已經非常棘手,所以如果任何人有任何建議,我會很感激!循環中的do_shortcode僅返回第一個帖子的內容

我的代碼是:

if($customposts->have_posts()) : while ($customposts->have_posts()) : $customposts->the_post(); 
    $post= get_the_ID(); 
    $foo = get_the_title(); 
    $bar=do_shortcode($content); 
    echo "<h2>".$foo."</h2><p>".$bar."</p>";  


endwhile; endif;  
+0

我猜測$內容是在循環外部設置的,因此你總是得到相同的結果。 – janh

+0

它在我的短代碼函數中設置在此循環外部。 – bjorkland

+0

'$ content'是什麼? – Spartacus

回答

0

它看起來並不像你需要給我們do_shortcode。如果title工作正常,您應該能夠將get_the_content()分配給您的$bar變量。

$bar = get_the_content(); 
0

既然你通過帖子循環,存儲從循環的每次迭代的HTML,然後返回所有的HTML一次(記住,你不能呼應或從短碼回調打印.. 。好了,你不應該,否則你會得到一些意想不到的結果):

$html = ''; 
    if($customposts->have_posts()) : 
     while ($customposts->have_posts()) : $customposts->the_post(); 
     $post= get_the_ID(); 
     $foo = get_the_title(); 
     $bar = get_the_content(); 
     $html .= "<h2>$foo</h2><p>$bar</p>"; 
    endwhile; endif; 

return $html; 

此外,要小心使用$post作爲自己的變量,你肯定會接觸到其他腳本衝突,包括核心。

+0

我正在使用短代碼..這個循環在我的短代碼函數中。在簡碼中使用the_content()也會導致致命錯誤。 – bjorkland

+0

我完全誤解了你的意圖。我編輯了我的答案。 – Spartacus

相關問題