Category based on post id

时间:2014-04-03 作者:kat

我有以下几点:

$custom_loop = new WP_Query( array(\'category_name\'=>$category, \'meta_key\'=>\'name\', \'orderby\' => \'meta_value\', \'order\' => \'ASC\', \'posts_per_page\'=>\'-1\'));
如果$post->ID == 1 然后$category == \'first\' 如果$post->ID == 2 然后$category == \'second\'

我想它应该是某种数组,但我不知道是什么?

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

WP_Query 它本身无法在内部执行该逻辑,但您可以轻松地动态创建查询。

$args = array(
  \'meta_key\' => \'name\', 
  \'orderby\' => \'meta_value\', 
  \'order\' => \'ASC\', 
  \'posts_per_page\' => \'-1\'
);

if ($post->ID == 1) {
  $args[\'category_name\'] = \'first\';
} elseif ($post->ID == 2) {
  $args[\'category_name\'] = \'second\';
}

$custom_loop = new WP_Query($args);

结束