在WordPress中排除循环中的粘性帖子和WP_QUERY()?
-
16-10-2019 - |
题
以下片段来自列出“最近帖子”的侧边栏小部件。由于它在主页上,而且我在该页面上出色地发表了我最后的粘性帖子,所以我想跳过此循环中的粘性。但是,那 post_not_in=sticky_posts
没有效果。
<?php
$the_query = new WP_Query("showposts=$number&offset=1&order=ASC&post_not_in=sticky_posts");
while ($the_query->have_posts()) : $the_query->the_post();
$do_not_duplicate = $post->ID; ?>
解决方案
我拿了 @tnorthcutt 回答 wordpress'codex on query_posts()
关于粘性参数 并创建了一个tandalone示例,您可以删除 test.php
进入您网站的根源,并通过导航到这样的URL来查看它,您的域被替换为:
关于代码的一些注释;我不得不使用 等同于查询字符串的数组 你过去了 WP_Query()
因为 post__no_in
不能以逗号界的字符串(不确定为什么可能是一个监督?)来传递论点。
我也想确保您知道从 offset=1
(代替 offset=0
)意味着您将排除第一篇文章,否则查询将返回。当然,您仍然会得到指定的帖子数量 $number
假设您有许多适用的帖子+1。因此,这是代码:
<?php
header('Content-Type:text/plain');
include "wp-load.php";
$number = 5;
$the_query = new WP_Query(array(
'showposts' => $number,
'offset' => 1, // This will cause the query to skip over first post
'order' => 'ASC',
'post__not_in' => get_option("sticky_posts"),
));
while ($the_query->have_posts()) : $the_query->the_post();
the_title();
endwhile;
其他提示
如果您想从查询中排除所有粘性帖子,请使用
query_posts(array("post__not_in" =>get_option("sticky_posts")));
(从 法典)
看起来只能在3.0或更高的情况下工作: http://wordpress.org/support/topic/excluding-sticky-posts-using-query_posts
编辑:为了回应您的评论,请尝试此操作(我不确定这会起作用,但希望它会让您入门):
<?php
$args=array(
'showposts'=>'$number',
'offset'=>'1',
'order'=>'ASC',
'post__not_in'=>get_option("sticky_posts")
);
$the_query = new WP_Query($args);
while ($the_query->have_posts()) : $the_query->the_post();
$do_not_duplicate = $post->ID; ?>