【问题标题】:Wordpress Wp_query object with 2 posts prints only one带有 2 个帖子的 Wordpress Wp_query 对象仅打印一个
【发布时间】:2013-04-16 21:19:15
【问题描述】:

我需要以另一种 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; }

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

【问题讨论】:

  • 谢谢你的更正尼克,英语不是我的母语。

标签: php wordpress loops


【解决方案1】:

试试这样

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>';
            }

【讨论】:

    【解决方案2】:

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

    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 循环没有得到最后一个帖子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多