我有一个自定义的帖子类型归档(archive-rg\\u blog.php),其中有两个循环。我想用下面的代码过滤其中一个循环。这只是所有帖子的基本循环,我只需要将其设置为返回所有帖子,而不是默认情况下返回的自定义帖子。
另一个循环需要从特定的自定义帖子中提取标题和内容。下面的代码似乎也在过滤第二个循环,因此它根本不返回任何内容。
function blog_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_post_type_archive(\'rg_blog\')) {
$query->set(\'post_type\', \'post\');
}
}
}
add_action(\'pre_get_posts\',\'blog_filter\');
这是一个不返回任何内容的循环:<?php // blog title and description query
$post_id = 4606;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;?>
<h1><?php echo $title;?></h1>
编辑:我只是想澄清一下,这就是我想要做的。///loop 1, queries a specific post
///loop 2, a normal loop filtered with pre_get_posts, which should return all posts.
///loop 3, also queries a specific post
所以我需要在循环2之前添加过滤器,然后在循环3之前再次删除它。我尝试在适当的位置将过滤器添加到存档模板中,但循环2无法返回所有帖子。编辑#2:下面是整个循环的运行情况。这是需要过滤的第二个循环。
<?php get_header(); ?>
<?php // blog title and description query
$post_id = 4606;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;?>
<h1><?php echo $title;?></h1>
<!-- Main Loop (All Posts) -->
<?php function blog_filter($query) {
if ( !is_admin() && $query->is_main_query()) {
if ($query->is_post_type_archive(\'rg_blog\')) {
$query->set(\'post_type\', \'post\');
}
}
}
add_action(\'pre_get_posts\',\'blog_filter\');?>
<?php $post = $posts[0]; $c=0;?>
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<!--Featured Post-->
<?php $c++;
if( !$paged && $c == 1) :?>
<!--stuff-->
<!--Other Posts-->
<?php else :?>
<!--the remaining posts-->
<?php endif;?>
<?php endwhile; endif; ?>
<!-- The end of the loop -->
<?php remove_action(\'pre_get_posts\',\'blog_filter\');?>
<!--third loop, a repeat of loop 1-->
<?php // blog title and description query
$post_id = 4606;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;?>
<h1><?php echo $title;?></h1>
<?php echo $queried_post->post_content;?>
<?php get_footer() ?>