我需要显示3个类别中的最新帖子和最后一个带有另一个HTML格式的帖子。问题是最后一个类别只打印一个帖子,并在对象上停止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; }
为什么循环只打印一个帖子并停止?
答案 0 :(得分: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>';
}
答案 1 :(得分: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循环没有收到最后一篇文章。