我正在使用此代码显示最近的帖子,我在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