在我的子主题中,我很难从主索引循环中排除特定页面。
我在函数中找到了以下函数。修改索引的php。php循环,以便显示页面而不是帖子:
add_action( \'pre_get_posts\', \'pages_loop_on_index\' );
function pages_loop_on_index( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( \'post_type\', array( \'page\' ) );
    $query->set( \'posts_per_page\', -1 );
}
}
 到目前为止,一切顺利。
我想让循环忽略页面ID 34,但我无法让这部分工作。
我尝试使用:
$query->set( \'category__not_in\', \'34\' );
 仍然会出现。
我尝试过:
$query->set( \'exclude\', \'34\' );
 但页面仍会显示出来。
我做错了什么?
这是索引的代码。php:
<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
    <?php if ( have_posts() ) : ?>
        <?php /* Start the Loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>
            <?php
                /* Include the Post-Format-specific template for the content.
                 * If you want to override this in a child theme, then include a file
                 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                 */
                get_template_part( \'content\', get_post_format() );
            ?>
        <?php endwhile; ?>
        <?php boardwalk_paging_nav(); ?>
    <?php else : ?>
        <?php get_template_part( \'content\', \'none\' ); ?>
    <?php endif; ?>
    </main><!-- #main -->
</div>
 
                    最合适的回答,由SO网友:Tim Malone 整理而成
                    你需要WP_Query documentation. WP\\u Query非常强大,您可以使用它做很多事情:)
就你而言,我不认为exclude 是有效参数。您可能正在寻找的是:
$query->set( \'post__not_in\', array( 34 ) );
Here\'s the Post & Page Parameters section 包括此参数和您可以使用的其他参数。
(顺便说一下,你试过的另一个,category__not_in, 将仅从类别ID 34中排除帖子,而不包括具有特定ID 34的帖子/页面)
顺便说一下,您还应该添加!is_admin() 检查您的功能,否则您可能会发现此页面也开始被排除在管理区域的显示之外。