Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 排除类别Wordpress(WP_查询不工作)_Php_Wordpress - Fatal编程技术网

Php 排除类别Wordpress(WP_查询不工作)

Php 排除类别Wordpress(WP_查询不工作),php,wordpress,Php,Wordpress,有人能解释一下为什么这个查询不起作用吗? 我想排除带有主页标签的帖子。 它仍然显示类别名称为“主页”的帖子 <?php $query = new WP_Query( 'category_name=-homepage'); ?> <?php if ( $query->have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <?php

有人能解释一下为什么这个查询不起作用吗? 我想排除带有主页标签的帖子。 它仍然显示类别名称为“主页”的帖子

<?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; ?>

如文档所示,在排除类别的情况下,您必须使用其ID,而不是slug检查

你可以试试:

$query = new WP_Query( array( 'category__not_in' => array( 11 ) ) );

如文档所示,在排除类别的情况下,您必须使用其ID,而不是slug检查

你可以试试:

$query = new WP_Query( array( 'category__not_in' => array( 11 ) ) );

代码中有两个问题

您正在使用slug而不是ID来排除类别,并且在自定义查询中没有正确使用循环

<?php
$query = new WP_Query( array(
    'cat' => -5, // replace with correct category ID. 
) );

if ( $query->have_posts() ) :

    // make sure we use have_posts and the_post method of our custom query.
    while ( $query->have_posts() ) : $query->the_post();
        get_template_part( 'content', 'news' );
    endwhile;

else:
    get_template_part( 'content', 'none' );
endif;
如果超出了初始问题的范围,则无法在自定义循环中使用\u posts\u导航。它作用于全局$wp_查询。我想你可能想看看pre_get_posts过滤器

进一步阅读:


代码中有两个问题

您正在使用slug而不是ID来排除类别,并且在自定义查询中没有正确使用循环

<?php
$query = new WP_Query( array(
    'cat' => -5, // replace with correct category ID. 
) );

if ( $query->have_posts() ) :

    // make sure we use have_posts and the_post method of our custom query.
    while ( $query->have_posts() ) : $query->the_post();
        get_template_part( 'content', 'news' );
    endwhile;

else:
    get_template_part( 'content', 'none' );
endif;
如果超出了初始问题的范围,则无法在自定义循环中使用\u posts\u导航。它作用于全局$wp_查询。我想你可能想看看pre_get_posts过滤器

进一步阅读:


还尝试了$query=new WP_query'cat=-11';但是它也不起作用。不相关的…但是老兄,去掉所有的标签…你还需要在发布帖子时进行更改:发布帖子;while$query->have_posts:$query->the_post;。谢谢你,内森!这似乎就是问题所在!我忽略了它@我怎么能这么做?还在学习中…还尝试了$query=new WP_query'cat=-11';但是它也不起作用。不相关的…但是老兄,去掉所有的标签…你还需要在发布帖子时进行更改:发布帖子;while$query->have_posts:$query->the_post;。谢谢你,内森!这似乎就是问题所在!我忽略了它@我怎么能这么做?还在学习中…你确定没有任何主题设置可以覆盖排除类别吗?谢谢Nathan和Grzegorz!你确定没有任何主题设置可以覆盖排除类别吗?谢谢Nathan和Grzegorz!