置顶帖,然后是普通帖,但数量有限(在 wp 查询中) [英] Sticky posts, then normal posts, but limited amount (in wp query)

查看:42
本文介绍了置顶帖,然后是普通帖,但数量有限(在 wp 查询中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我正在处理的项目的第一页上,我需要显示来自我的自定义帖子类型项目"的总共三篇帖子.

So on the first page of the project of which I'm working on I need to display a total of three posts from my custom post type 'project'.

这种帖子类型也有一些我想先显示的置顶帖.然后是常规的.但重要的是,无论粘性多少,帖子的数量都是三个.

This post type also have some sticky posts which I wish to display first. And then the regular ones after. But it is important that the number of posts are three regardless of sticky amount.

我找到了很多例子,但我做对了.例如,有没有一种方法可以先查询置顶帖子并将其限制为三个,然后再进行第二个查询将帖子数量限制为三个减去置顶帖子的数量?

I've found a bunch of examples, but I can't get it right. Is there for example a way to query sticky posts first and limit those to three, and have a second query that limits the post number to three minus amount of sticky posts?

推荐答案

简单的方法是在两个查询中进行,尝试(两个 args 上都存在 'ignore_sticky_posts'.不是错误,只是顺序),已测试:

Easy way is to do in two queries, try (presence of 'ignore_sticky_posts' on both args. is not a mistake, it is just about the order), was tested:

$sticky = get_option( 'sticky_posts' );
$args = array(
 'ignore_sticky_posts' => 1,
  'post__in' => $sticky,
  'posts_per_page' => 3,
);

$the_query = new WP_Query( $args );
$total_posts_onpage = 5;
$nuber_of_noSticky = $total_posts_onpage - $the_query->found_posts;

// code to display sticky ones
wp_reset_postdata();

//second query
$args = array(
 'ignore_sticky_posts' => 1,
 'post__not_in' => $sticky,
 'posts_per_page' => $nuber_of_noSticky,
);

$the_query = new WP_Query( $args );

这篇关于置顶帖,然后是普通帖,但数量有限(在 wp 查询中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆