我需要使用另一种 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; }
为什么循环只打印一篇文章并停止?