排除类别Wordpress(WP_Query无法正常工作)

Mat*_*iet 1 php wordpress wp-query

任何人都可以解释为什么这个查询不起作用?我想排除用主页标记的帖子.它仍显示类别名称为"主页"的帖子...

<?php
    $query = new WP_Query( 'category_name=-homepage');
?>

<?php if ( $query->have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
        <?php
            get_template_part( 'content', 'news' );
        ?>
    <?php endwhile; ?>
    <?php the_posts_navigation(); ?>
    <?php else : ?>
        <?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)

Grz*_*lik 8

如果在排除类别的情况下在文档中给出,您必须使用其ID而不是slug(请在此处查看).

你可以尝试:

$query = new WP_Query( array( 'category__not_in' => array( 11 ) ) );
Run Code Online (Sandbox Code Playgroud)