首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何显示最大。当posts_per_page为3时,会有2个粘帖吗?

如何显示最大。当posts_per_page为3时,会有2个粘帖吗?
EN

WordPress Development用户
提问于 2018-10-02 01:01:26
回答 1查看 710关注 0票数 0

我对贴纸有问题。这是我的循环:

代码语言:javascript
运行
复制
 3,
    'orderby' => 'date',
);
$query = new WP_Query($main_loop);
?>
     have_posts()): ?>
     have_posts()): $query->the_post();?>

// loop

现在,当posts_per_page是3,我需要显示最大。2个粘性柱。所以:

当1粘的时候也有2条固定的帖子,当2粘的时候也有1条固定的帖子,当0粘粘的时候有3条固定的帖子。

我怎么发动汽车呢?

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2018-10-02 09:56:54

编辑2:更新后的代码,以说明“也许没有粘贴的帖子”;)

编辑:我用帖子的数量做了个嘘声。修正了,现在应该按预期工作了。

您可以通过使用两个查询来完成这一任务。

代码语言:javascript
运行
复制
 count($stickiessliced),
        'orderby' => 'date',
        'post__in' => $stickiessliced,
        'ignore_sticky_posts' => true
    );
    //only get sliced sticky posts, ignore everything else
    $query = new WP_Query($sticky_loop);
    //do the loop for the sticky posts
    if ($query->have_posts()): ?>
         have_posts()): $query->the_post();?>

    // loop

         
         
     $numposts,
    'orderby' => 'date',
    'post__not_in' => $stickies,
    //replace $stickiessliced with $stickies to make sure that NO other stickies are queried (as is: the above queried stickies are ignored)
    'ignore_sticky_posts' => true
);
$query = new WP_Query($main_loop);
//do the loop for the remaining posts to fill up
if ($query->have_posts()): ?>
     have_posts()): $query->the_post();?>

// loop

这应该是这样的:如果有0到2个粘性的帖子,他们会被查询,循环,然后1到3个“正常”的帖子被查询。如果有两个以上的粘性帖子,前两个是切片,然后再一个“正常”的帖子被查询。

编码愉快!

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/315680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档