首页 文章

Wordpress查询仅显示内容不为空的帖子[重复]

提问于
浏览
2

这个问题在这里已有答案:

我正在使用查询在我的主页上随机显示1个自定义帖子类型 . 我没有运气,试过"meta_query" . As covered here.

我还玩过其他的东西,比如如果内容是空的,试图在循环中获得下一篇文章,但也无法找到办法 .

如果内容为空,我试图获得下一篇文章 . 但我不认为“get_next_post();”这样工作 .

$loop = new WP_Query( array(
        'post_type' => 'custom',
        'post_status' => 'publish', 
        'posts_per_page' => 1,
        'orderby' => 'rand',
        'order' => 'DESC',
    ) );
while ( $loop->have_posts() ) : $loop->the_post();

if($post->post_content=="") {
get_next_post();
} else {
the_title();
the_content();
};
endwhile;

任何建议,将不胜感激 .

1 回答

  • 2

    像这样基本的东西应该适合你 . 首先修剪帖子内容可确保您不会无意中包含只有空格的帖子 .

    while ( $loop->have_posts() ) : $loop->the_post();
        if(trim($post->post_content) !== "") {
            the_title();
            the_content();
        };
    endwhile;
    

相关问题