Wordpress WP_Query,在category_name中,但不在特定类别中

时间:2013-11-04 15:09:03

标签: php wordpress

您有一个包含儿童类别的类别。我想显示几乎所有类别,除了一些..

这就是我想要实现的目标:

$slug1 = '1998';
$slug2 = '1999';
$slug3 = '2000';
$slug4 = '2001';
$slug5 = '2002';
$cat1 = get_category_by_slug($slug1);
$cat2 = get_category_by_slug($slug2);
$cat3 = get_category_by_slug($slug3);
$cat4 = get_category_by_slug($slug4);
$cat5 = get_category_by_slug($slug5);

$the_query = new WP_Query( array ( 
'orderby' => 'rand', 
'posts_per_page' => '1', 
'category_name' => 'idols',
'category__not_in' => -$cat1, -$cat2, -$cat3, -$cat4, -$cat5
 ) );

但是不行......有什么想法?

2 个答案:

答案 0 :(得分:1)

category__not_in' => -$cat1, -$cat2, -$cat3, -$cat4, -$cat5

应该是

category__not_in' => array(-$cat1, -$cat2, -$cat3, -$cat4, -$cat5)

答案 1 :(得分:1)

这解决了问题!

$slug1 = '1998';
$slug2 = '1999';
$slug3 = '2000';
$slug4 = '2001';
$slug5 = '2002';
$cat1 = get_category_by_slug($slug1);
$cat2 = get_category_by_slug($slug2);
$cat3 = get_category_by_slug($slug3);
$cat4 = get_category_by_slug($slug4);
$cat5 = get_category_by_slug($slug5);
$id1 = $cat1->term_id;
$id2 = $cat2->term_id;
$id3 = $cat3->term_id;
$id4 = $cat4->term_id;
$id5 = $cat5->term_id;


$the_query = new WP_Query( array ( 
'orderby' => 'rand', 
'posts_per_page' => '1', 
'category_name' => 'idols',
'category__not_in' => array($id1, $id2, $id3, $id4, $id5)
 ) );