wordpress - category__not_in 不起作用 [英] wordpress - category__not_in not working

查看:38
本文介绍了wordpress - category__not_in 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在获取查询功能时遇到问题.我需要运行循环,排除特定类别.

I'm having a problem getting my query function. I need to run the loop, excluding a particular category.

我正在尝试使用 category__not_in,但在某些情况下根本不起作用.

I'm trying to use category__not_in, but is not working at all some.

<?php
  $args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category__not_in' => array( '44' ),
    'posts_per_page' => 9,
    'paged' => get_query_var('paged')
  );
  $query = new WP_Query( $args );

  query_posts($query);
?>

我已经试过了:

'category__not_in' => array( '44' ),
'category__not_in' => array( 44 ),
'category__not_in' => '44',
'category__not_in' => 44,

但没有任何效果=(

推荐答案

谢谢各位,感谢 @rnevius

问题出在我的查询中,我使用了 WP_Query()query_posts().

The problem was in my query, I was using WP_Query() and query_posts().

我使用了 WP Codex 的参考方式:https://codex.wordpress.org/Class_Reference/WP_Query

I used how reference the WP Codex: https://codex.wordpress.org/Class_Reference/WP_Query

下面是我的代码最后的样子:

Below is how my code was at the end:

<?php
  $args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category__not_in' => array( 44 ),
    'posts_per_page' => 9,
    'paged' => get_query_var('paged')
  );
  $query = new WP_Query( $args );
?>

<?php
  if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
      $query->the_post();
?>

// code

<?php
    }
  } else {
    // no posts found
  }
  wp_reset_postdata();
?>

这篇关于wordpress - category__not_in 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆