foreach循环正常工作,但category_name不在Wp_Query内部循环:({

时间:2019-09-09 04:52:16

标签: php wordpress foreach

我想从帖子中查询类别。类别显示在输出中,但是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',
];

2 个答案:

答案 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',
];