Php Wordpress Wp_查询对象有两篇文章,只打印一篇
我需要显示最新的文章从3个类别和两个从这最后一个与另一个HTML格式的帖子。问题是最后一个类别只打印一篇文章,并在我可以看到这两篇文章的对象上停止使用Php Wordpress Wp_查询对象有两篇文章,只打印一篇,php,wordpress,loops,Php,Wordpress,Loops,我需要显示最新的文章从3个类别和两个从这最后一个与另一个HTML格式的帖子。问题是最后一个类别只打印一篇文章,并在我可以看到这两篇文章的对象上停止使用var\u dump() 检查此处的功能: function destaques( $atts ) { extract( shortcode_atts( array( 'id' => 0, ), $atts ) ); $id = array(6,16,10,4); $posts = array(); $nomesCat = arra
var\u 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;
函数destaques($atts){
提取(短码)附件(数组)(
“id”=>0,
)美元(附件);;
$id=数组(6,16,10,4);
$posts=array();
$nomesCat=array();
foreach($id作为$key=>$value){
如果($value==4){
//这是一个有两个帖子的分类
$posts[]=新的WP_查询(数组('posts_per_page'=>2,'category_in'=>array($value));
}否则{
$posts[]=新的WP_查询(数组('posts_per_page'=>1,'category_in'=>array($value));
}
$nomesCat[]=获取类别($value);
}
$html='';
foreach($key=>$value){
如果($value->have_posts()){
而($value->have_posts()){
$value->the_post();
如果($nomesCat[$key]->cat_name=='Colunistas'){
//检查分类名称,然后调用另一个
//函数,传递wp_查询对象
$html.=auxiliarColunistas($value);
打破
}否则{
//很多html格式代码
$html.='';
}
}
}
}
$html.='
';
返回$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; }
功能辅助Colunistas($posts){
$html='';
/*var_dump($posts);返回两个posts!
死亡*/
$html.='Colunistas';
如果($posts->have_posts()){
而($posts->have_posts()){
$posts->the_post();
//更多html格式代码
}
}
$html.=' ';
返回$html;}
为什么循环只打印一篇文章并停止?试着这样做
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>';
}
foreach($id作为$key=>$value){
如果($value==4){
//这是一个有两个帖子的分类
$posts\u query=新的WP\u查询(数组('posts\u per\u page'=>2,'category\u in'=>array($value));
}否则{
$posts\u query=新的WP\u查询(数组('posts\u per\u page'=>1,'category\u in'=>array($value));
}
$nomesCat[]=获取类别($value);
}
$html='';
而($posts\u query->have\u posts()){
如果($nomesCat[$key]->cat_name=='Colunistas'){
//检查分类名称,然后调用另一个
//函数,传递wp_查询对象
$html.=auxiliarColunistas($posts\u query->the\u post());
打破
}否则{
//很多html格式代码
$html.='';
}
我可以通过改变这一行来解决它:
if ($value == 4){
//this is the categorie with two posts
$posts_query = new WP_Query( array('posts_per_page' => 2, 'category__in' => array($value)));
到
但是我还是不明白为什么while循环没有得到最后一篇文章。谢谢你的更正,尼克,英语不是我的母语。
if ($value == 4){
//this is the categorie with two posts
$posts_query = new WP_Query( array('posts_per_page' => 3, 'category__in' => array($value)));