从每个类别中查询最多 4 个帖子
Query max 4 posts from each category
我被创建了 custom post type
dishmenu
。我的 dishmenu
中有 4 个类别,例如 Special,Breakfast,Lunch,Dinner
。现在我想从每个类别中获取最多 4 条记录(如果可用)。
这是我到目前为止尝试过的方法:
<?php
$menucat = get_terms('menu_category');
foreach($menucat as $category){
$menuQuery = new WP_Query(array('post_type'=>'dishmenu','posts_per_page'=>4,'category_name'=>$category->slug));
if($menuQuery->have_posts()):
while($menuQuery->have_posts()): $menuQuery->the_post();
$price = json_decode(json_encode(get_post_meta($menuQuery->post->ID)),false);
echo '<div class="element-item '.$category->slug.' col-sm-6" data-category="'.$category->slug.'" >
<div class="dish-menu-item">
<div class="dish-border-circle">
<div class="dish-menu-left"></div>
</div>
<div class="dish-menu-body">
<h4>'.get_the_title().'<span class="pull-right"><span class="error-text">'.$price->Price[0].'</span><sub>$</sub></span></h4>
<p>'.get_the_content().'</p>
</div>
<div class="dish-menu-right text-center">
<p style="padding:2px; display:inline-block;"></p>
</div>
</div>
</div>';
endwhile;
wp_reset_postdata();
else:
echo '<div class="element-item '.$category->slug.' col-sm-6" data-category="'.$category->slug.'" >
No posts found in '.$category->slug.'
</div>';
endif;
}
?>
现在的问题是每次else块都被执行了。
您需要在WP_Query
中使用tax_query
查询
请检查以下查询
$arg=array(
'post_type'=>'dishmenu',
'posts_per_page'=>4,
'tax_query' => array(
array('taxonomy' => 'menu_category',
'field' => 'slug',
'terms' => $category->slug,
'include_children' => true)
)
);
$menuQuery = new WP_Query($arg);
我被创建了 custom post type
dishmenu
。我的 dishmenu
中有 4 个类别,例如 Special,Breakfast,Lunch,Dinner
。现在我想从每个类别中获取最多 4 条记录(如果可用)。
这是我到目前为止尝试过的方法:
<?php
$menucat = get_terms('menu_category');
foreach($menucat as $category){
$menuQuery = new WP_Query(array('post_type'=>'dishmenu','posts_per_page'=>4,'category_name'=>$category->slug));
if($menuQuery->have_posts()):
while($menuQuery->have_posts()): $menuQuery->the_post();
$price = json_decode(json_encode(get_post_meta($menuQuery->post->ID)),false);
echo '<div class="element-item '.$category->slug.' col-sm-6" data-category="'.$category->slug.'" >
<div class="dish-menu-item">
<div class="dish-border-circle">
<div class="dish-menu-left"></div>
</div>
<div class="dish-menu-body">
<h4>'.get_the_title().'<span class="pull-right"><span class="error-text">'.$price->Price[0].'</span><sub>$</sub></span></h4>
<p>'.get_the_content().'</p>
</div>
<div class="dish-menu-right text-center">
<p style="padding:2px; display:inline-block;"></p>
</div>
</div>
</div>';
endwhile;
wp_reset_postdata();
else:
echo '<div class="element-item '.$category->slug.' col-sm-6" data-category="'.$category->slug.'" >
No posts found in '.$category->slug.'
</div>';
endif;
}
?>
现在的问题是每次else块都被执行了。
您需要在WP_Query
tax_query
查询
请检查以下查询
$arg=array(
'post_type'=>'dishmenu',
'posts_per_page'=>4,
'tax_query' => array(
array('taxonomy' => 'menu_category',
'field' => 'slug',
'terms' => $category->slug,
'include_children' => true)
)
);
$menuQuery = new WP_Query($arg);