我知道pre_get_posts
可用于在主站点、自定义帖子类型和页面上进行查询。但是,有没有一种方法可以在侧边栏中定位查询?现在,我使用的是“is\\u page\\u template()”,但它只使用主查询。
my上的代码functions.php
模板:
function opby_query( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set(\'tax_query\', array(
array(\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => array( \'podcast-control-daily\' ),
\'operator\'=> \'NOT IN\'
),
array(\'taxonomy\' => \'post_format\',
\'field\' => \'slug\',
\'terms\' => array( \'post-format-status\' ),
\'operator\'=> \'NOT IN\')
)
);
$query->set( \'posts_per_page\', 15 );
}
if ($query->is_page_template(\'sidebar.php\')) {
$query->set( \'tax_query\', array(
array(\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => array( \'podcast-control-daily\' ),
\'operator\'=> \'IN\'
)));
$query->set( \'posts_per_page\', 5 );
}
return $query;
}
add_action( \'pre_get_posts\', \'opby_query\' );
侧栏使用默认循环:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title();?><br>
<?php endwhile; else : ?>
<?php endif; ?>