是否确实要以这种方式执行此操作?通常,这样的节可能会以某种方式写入页面模板,而不是通过内容编辑器调用。也就是说,这应该满足您的需要:
add_shortcode( \'show_xyz_news\', \'xyz_news_query\' );
function xyz_news_query() {
$args = array(
\'posts_per_page\' => 3,
\'category_name\' => \'news\',
);
$news_query = new WP_Query( $args );
if ( $news_query->have_posts() ) :
$html_out = \'<ul>\';
while ( $news_query->have_posts() ) :
$news_query->the_post();
// Do stuff with each post here
$html_out .= \'<li><a href="\' . get_permalink() . \'">\' . get_the_title() . \'</a></li>\';
endwhile;
$html_out .= \'</ul>\';
else : // No results
echo "Nothing to show";
endif;
wp_reset_query();
return $html_out;
}
然后在页面编辑器中调用短代码
[show_xyz_news]
.