WP_QUERY不使用变量

时间:2017-01-04 作者:Kirk

如果我使用此代码(代码不完整,这是问题部分),帖子会正确显示。注意“新的WP\\U查询(\'cat=2,3,5,6,7,8,9”);”:

$catslugs = array(\'academic\',/*\'adventure\',*/ \'arts\', \'religious\', \'special-interest\', \'special-needs\', \'sports\',\'teen\');//Must upate as new Main categories are added 
    $out = array();
    foreach($catslugs as $slug) {
        array_push($out, get_category_by_slug($slug)->term_id);
        $CatIDs= implode(\',\', $out);
        }
    //featured category ID = 153
    //academic category ID = 2
    //adventure category ID = 4
    //arts category ID = 3
    //religious category ID = 5
    //special-interest category ID = 6
    //special-needs category ID = 7
    //sports category ID = 8
    //teen category ID = 9
    $CatIDsCode="\'cat=".$CatIDs."\'";
    $camp_posts_bynumber = new WP_Query(\'cat=2,3,5,6,7,8,9\');

    if( $camp_posts_bynumber->have_posts() ):
如果我把它改成这个,它不会。相反,它会显示所有类别,包括“未分类”。我做错了什么?是不是写着“have\\u posts()”的东西

$catslugs = array(\'academic\',/*\'adventure\',*/ \'arts\', \'religious\', \'special-interest\', \'special-needs\', \'sports\',\'teen\');//Must upate as new Main categories are added 
$out = array();
foreach($catslugs as $slug) {
    array_push($out, get_category_by_slug($slug)->term_id);
    $CatIDs= implode(\',\', $out);
    }
//featured category ID = 153
//academic category ID = 2
//adventure category ID = 4
//arts category ID = 3
//religious category ID = 5
//special-interest category ID = 6
//special-needs category ID = 7
//sports category ID = 8
//teen category ID = 9
$CatIDsCode="\'cat=".$CatIDs."\'";
$camp_posts_bynumber = new WP_Query($CatIDsCode);

if( $camp_posts_bynumber->have_posts() ):

$catslugs = array(\'academic\',/*\'adventure\',*/ \'arts\', \'religious\', \'special-interest\', \'special-needs\', \'sports\',\'teen\');//Must upate as new Main categories are added 
$out = array();
foreach($catslugs as $slug) {
    array_push($out, get_category_by_slug($slug)->term_id);
    $CatIDs= implode(\',\', $out);
    }
//featured category ID = 153
//academic category ID = 2
//adventure category ID = 4
//arts category ID = 3
//religious category ID = 5
//special-interest category ID = 6
//special-needs category ID = 7
//sports category ID = 8
//teen category ID = 9
$CatIDsCode="\'cat=".$CatIDs."\'";
$camp_posts_bynumber = new WP_Query($CatIDs);

if( $camp_posts_bynumber->have_posts() ):

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

我很确定这是因为在设置$CatIDsCode.

所以这应该可以解决它:

$camp_posts_bynumber = new WP_Query (array (\'cat\' => $CatIDs));
P.S.移动$CatIDs= implode(\',\', $out);foreach 回路:)

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post