WP_QUERY在循环内不起作用

时间:2016-12-29 作者:Marcos

我试图用这个结构(whit this content())显示一个类别及其子类别:

 - Category Father title
     - Children title
     - Other children title
 - Category Father title
     - ....
但是用这个代码

$catH = array(11);
$query = array("cat"=>42,"order"=>"ASC","posts_per_page"=>-1,"category__not_in"=>$catH);
$obj = new WP_Query($query);
if ($obj->have_posts()){    
    while($obj->have_posts()){
        $obj->the_post();
        $slug = get_the_tags();         
        the_title();
        echo "<br/>";
        $queryH = array("cat"=>13,"order"=>"ASC","posts_per_page"=>-1,"tag__in"=>$slug);
        $objH = new WP_Query($queryH);
        if ($objH->have_posts()){
            while($objH->have_posts()){
                $objH->the_post();
                the_title();
                echo "<br/>";
            }
        }
    }
    wp_reset_postdata();    
}
结果如下:

类别父标题

儿童标题

其他孩子的标题如何修复?

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

try like this:

if ($obj->have_posts()){    
    while($obj->have_posts()){
        $obj->the_post();
        $slug = get_the_tags();         
        $obj->the_title();
        echo "<br/>";
        $queryH = array("cat"=>13,"order"=>"ASC","posts_per_page"=>-1,"tag__in"=>$slug);
        $objH = new WP_Query($queryH);
        if ($objH->have_posts()){
            while($objH->have_posts()){
                $objH->the_post();
                $objH->the_title();
                echo "<br/>";
            }
        }
    }  
}