在单个帖子页面上,我有一个侧栏显示最多三个其他相关帖子。如何排除Sticky Posts和Current post?
我知道如何在Current post
中使用post_not_in排除Sticky Posts
以及如何排除WP_Query
,请参阅下面的代码示例。但我想你不能在同一个查询中使用post__not_in
两次。有什么建议吗?
$current_post_ID = get_the_ID();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 3,
'post__not_in' => get_option( 'sticky_posts' )
'post__not_in' => array($current_post_ID)
);
答案 0 :(得分:0)
post__not_in
是数组()并使用帖子ids
(仅限数字)。
没有必要使用它两次,使用它:
//First build the array of excluded posts
$excluded_posts = array_push(get_option( 'sticky_posts' ) , $current_post_ID);
//Then use it in the option
'post__not_in' => array( $excluded_posts )
注意array_push()
PHP函数,这会将当前帖子添加到sticky posts数组的末尾,然后传递给'post__not_in'
选项。
答案 1 :(得分:0)
<?php
$sticky =array(get_option('sticky_posts'));
// (add post id on sticky_posts option like ex. 485,458,256)
$current_post_ID = get_the_ID();
array_push($sticky,$current_post_ID);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 3,
'post__not_in' => array($sticky)
);
query_posts($args);
while ( have_posts() ) : the_post(); ?>
<!-- add your code which to display hear -->
<?php
endwhile;
wp_reset_query();
?>