我有一个包含以下ID的帖子 $ posts_id =阵列(1,2,3,4,5); 我不想在帖子页面上显示每个帖子。我想从帖子页面中排除一些帖子ID。举例来说,我想删除显示的2和3的帖子ID。 我如何在wordpress中实现这一点。这就是我的全部。
<?php while(have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile?>
<?php get_footer();?>
答案 0 :(得分:1)
使用pre_get_posts()这样的操作:
function exclude_single_posts_home($query) {
if ($query->is_home() && $query->is_main_query()) {
$query->set('post__not_in', array(2,3));
}
}
add_action('pre_get_posts', 'exclude_single_posts_home');
并将其放入您的functions.php
文件中。
这会影响您的主页(如果您已将自己的家设置为帖子页面而不home.php
,则默认页面模板应该回归到index.php
)。
同时检查template hierarchy。
答案 1 :(得分:0)
使用pre_get_posts()
操作。在主题function.php
文件
function exclude_post($query) {
if( is_home() ) {
//add condition in which page you don't want to display post.
//WordPress by default home page is post page
$query->set('post__not_in', array(2,3));
//add post id which you want to remove from listing ex: array(2,3)
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_post' );
编辑 从其他页面删除
使用is_page
(页面ID,标题,页面标题)从特定页面中删除帖子
function exclude_post($query) {
if( is_home() || is_page (Page ID, title, slug of page )) {
//add condition in which page you don't want to display post.
//WordPress by default home page is post page
$query->set('post__not_in', array(2,3));
//add post id which you want to remove from listing ex: array(2,3)
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_post' );
答案 2 :(得分:0)
您要查找的参数是post__not_in
。所以代码可能就像:
<?php
$my_query = new WP_Query(array(
'post__not_in' => array(278),
'post_type' => 'case-study',
'paged' => $paged,
));
while ($my_query->have_posts()) : $my_query->the_post(); endwhile;