0

我需要使用另一种 HTML 格式显示 3 个类别的最新帖子和最后一个类别的两个帖子。问题是最后一个类别只打印一个帖子并var_dump()在我可以看到两个帖子的对象上停止。

检查这里的功能:

function destaques( $atts ) {
extract( shortcode_atts( array(
    'id' => 0,
), $atts ) );

$id = array(6,16,10,4);
$posts = array();
$nomesCat = array();

foreach ($id as $key => $value) {
    if ($value == 4){
                    //this is the categorie with two posts
        $posts[] = new WP_Query( array('posts_per_page' => 2, 'category__in' => array($value)));
    } else {
        $posts[] = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($value)));    
    }
    $nomesCat[] = get_category($value);
}
$html = '<ul class="destaques">';

foreach ($posts as $key => $value) {
    if ($value->have_posts()){
        while($value->have_posts()){
            $value->the_post();
            if ($nomesCat[$key]->cat_name == 'Colunistas') {
                                    // check for the categorie name, then call another
                                    // function, passing the wp_query object
                $html .= auxiliarColunistas($value);
                break;
            } else {
                //lots of html formatting code
                $html .= '</li>';
            }
        }
    }


}

$html .= '</ul>';
return $html; 

这是辅助函数:

function auxiliarColunistas ($posts) {
$html = '<li class="last">';

/*var_dump($posts); this returns two posts!
die;*/

$html .= '<h2>Colunistas</h2>';

if ($posts->have_posts()){

    while ($posts->have_posts()) {
        $posts->the_post();
        //more html formatting code
    }
}
$html .= '</li>';

return $html; }

为什么循环只打印一篇文章并停止?

4

2 回答 2

0

我可以通过改变这一行来解决它:

if ($value == 4){
                //this is the categorie with two posts
    $posts_query = new WP_Query( array('posts_per_page' => 2, 'category__in' => array($value)));

if ($value == 4){
                //this is the categorie with two posts
    $posts_query = new WP_Query( array('posts_per_page' => 3, 'category__in' => array($value)));

但我仍然不明白为什么while循环没有得到最后一篇文章。

于 2013-04-17T14:03:54.907 回答
0

尝试这样做

foreach ($id as $key => $value) {
    if ($value == 4){
                    //this is the categorie with two posts
        $posts_query = new WP_Query( array('posts_per_page' => 2, 'category__in' => array($value)));
    } else {
        $posts_query = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($value)));    
    }
    $nomesCat[] = get_category($value);
}
$html = '<ul class="destaques">';

        while($posts_query->have_posts()){
            if ($nomesCat[$key]->cat_name == 'Colunistas') {
                                    // check for the categorie name, then call another
                                    // function, passing the wp_query object
                $html .= auxiliarColunistas($posts_query->the_post());
                break;
            } else {
                //lots of html formatting code
                $html .= '</li>';
            }
于 2013-04-17T08:45:21.690 回答