我想从帖子中查询类别。类别显示在输出中,但是category_name
没有在wp_query
数组中循环。我该如何解决?
您可以在下面看到我的代码:
public function minzel_blog_cats() {
$settings = $this->get_settings_for_display();
$blog_cats = $settings['blog_category'];
//if (!empty($blog_cats) && !is_wp_error($blog_cats)) {
foreach ($blog_cats as $blog_cat) {
//$cat_options[$blog_cat->slug] = $blog_cat->slug;
//$cat_options[$term->slug] = " '".$term->slug.", '";
$cat_options = $blog_cat." , ";
echo $blog_cat.", ";
}
//}
return $cat_options;
}
$default = [
'posts_per_page' => $blog_post_per,
'orderby' => $blog_grid_orderby,
'category_name' => " ' ".$this->minzel_blog_cats()." ', ",
'order' => $blog_grid_order,
'post_type' => 'post',
];
答案 0 :(得分:1)
您正在循环中一次又一次地覆盖$cat_options
,需要使用串联运算符(.
)分配所有名称
public function minzel_blog_cats() {
$settings = $this->get_settings_for_display();
$blog_cats = $settings['blog_category'];
$cat_options = '';//create an empty string
foreach ($blog_cats as $blog_cat) {
$cat_options .= $blog_cat." , "; //assign all names
}
return $cat_options;
}
答案 1 :(得分:0)
用下面的代码替换默认参数并检查。
$default = [
'posts_per_page' => $blog_post_per,
'orderby' => $blog_grid_orderby,
'category' => " ' ".$this->minzel_blog_cats()." ', ",
'order' => $blog_grid_order,
'post_type' => 'post',
];