【问题标题】:foreach loop working but category_name not looping inside of Wp_Query :(foreach 循环工作但 category_name 没有在 Wp_Query 内循环:(
【发布时间】:2019-09-09 04:52:16
【问题描述】:

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

【问题讨论】:

    标签: php wordpress foreach


    【解决方案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;
    }
    

    【讨论】:

      【解决方案2】:

      用下面的代码替换默认参数并检查。

      $default    = [
          'posts_per_page'    => $blog_post_per,
          'orderby' => $blog_grid_orderby,
          'category' => " ' ".$this->minzel_blog_cats()." ', ",
          'order' => $blog_grid_order,
          'post_type'         => 'post',
      ];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-24
        • 2016-09-26
        • 2012-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多