因此,我试图获得一个短代码工作,它调用的文章在特色故事类别,但我有问题,让它输出,甚至只是标题的文章。页面上没有错误。但除了导航和页脚之间的空白之外,没有其他任何东西显示。
function featured_story() {
$args = array(
'posts_per_page' => 5,
'category_name' => 'featured_story'
);
$last_5_posts_query = new WP_Query( $args );
while($last_5_posts_query->have_posts()) :
$last_5_posts_query->the_post();
$title = get_the_title();
$content .= '';
$content .= '' .$title. '';
endwhile;
return $content;
}
add_shortcode( 'featured-story', 'featured_story' );发布于 2020-11-10 13:07:31
第一件事:在while循环之前初始化变量$content='';。除此之外,你的代码是正确的。有件事你做错了使用短代码。您应该检查这个链接https://developer.wordpress.org/reference/functions/do_短代码/#用户贡献-注释并进行更改。
另外,如果使用自定义post类型,请在post_type中添加$args参数
https://wordpress.stackexchange.com/questions/377924
复制相似问题