我知道这个问题已经得到了回答,答案也解决了这个问题,但我发现对于任何想学习WordPress的新手来说都很糟糕,所以我给出了我的答案,希望它会更好:
当get_posts()
如果我能完成这项工作,我会使用simplequery,对于二次循环,我更喜欢创建自己的WP\\U查询独立实例Engelen是对的wp_reset_postdata()
这里使用$content
在这里也是不必要的,它不是一个封闭的短代码,短代码返回一些东西。它决不应该自己输出一些东西。这可能会引发不可预测的bug。这和add_filter()
add_shortcode( \'post_title\', \'wpse_149667_post_title_sc\' );
function wpse_149667_post_title_sc( $atts ){
$args = array( \'posts_per_page\' => 3 );
$lastposts = new WP_Query( $args );
if( $lastposts->have_posts() ) :
$output = \'<ul>\';
while( $lastposts->have_posts() ) : $lastposts->the_post();
$output .= \'<li>\'.get_the_title($lastposts->post->ID).\'</li>\';
endwhile;
$output .= \'</ul>\';
else :
$output = \'There is currently no post to retrieve!\';
endif;
wp_reset_postdata();// more appropriate here
return $output;
}
希望这能提供更多的解释。