我正在使用此代碼顯示最近的帖子,我在smashingmagazine網站發現的短代碼。這不是正確的方式,我的意思是當我指定要顯示的帖子數量時,它只顯示一個帖子和我指定的每個數字。WordPress的短代碼顯示不正確的帖子數
下面的代碼:
function recent_posts_function() {
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post();
$return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
endwhile;
endif;
wp_reset_query();
return $return_string;
}
function register_shortcodes(){
add_shortcode('recent-posts', 'recent_posts_function');
}
add_action('init', 'register_shortcodes');
我已經改變了showposts
數量,但沒有任何反應。怎麼了?
有什麼建議嗎?
'$ return_string'在每次迭代循環時都會被覆蓋,所以它只會從最後一行獲取結果。將其更改爲'$ return_string。='連續串聯字符串,生成一長串鏈接。 – Ohgodwhy 2014-08-28 03:26:34
好的,我修復了這部分,但現在帖子的數量仍然是錯誤的,當我把'showposts'=> 1,它顯示6個職位,當我把2顯示7職位,當我把3顯示8職位! !這裏發生了什麼? – Komeyl94 2014-08-28 03:49:57