Wordpress : 警告:urlencode() 期望参数 1 是字符串,数组以 wp-include/format



当我尝试按类别过滤自定义帖子类型存档时,我遇到了一个问题。

我想按多个类别过滤woocommerce的商店,所以我制作了一个小部件,显示一个包含所有类别的表单,每个类别都有一个复选框。

此表单的 HTMl 是:

<form method="get">
  <?php foreach($cats as $cat) : ?>
     <p>
        <input 
          style="margin-bottom: 0;" 
          type="checkbox" 
          id="<?php echo $cat->slug?>" 
          name="product_cat[]" 
          value="<?php echo $cat->slug?>">
          <label style="margin: 5px 0 0 0; color: #777;" 
                 for="<?php echo $cat->slug?>" >
            <?php echo $cat->name; ?> (<?php echo $cat->count ?>)
          </label>
       </p>
   <?php endforeach; ?>
   <button class="button primary" style="margin-top: 20px;" type="submit">
     <?php _e('Filtrer', 'themedomain');?>
   </button>
</form>

当我选中一个或多个复选框并提交表单时,我得到这个网址

http://example.com/shop/?product_cat%5B%5D=chauffageclimatisation&product_cat%5B%5D=essencegaz

这对我来说似乎很棒,但我收到此错误消息并且没有显示任何帖子:

Warning: urlencode() expects parameter 1 to be string, array given in /path/to/website/wp-includes/formatting.php on line 4791

我很确定这必须有效,因为我在以前的工作中已经完成了它,但我无法访问代码来检查它。有人可以帮助我吗?

我还知道,如果您在查询中添加与','分开的术语,WordPress 会自动过滤帖子,例如:

http://example.com/shop/?product_catchauffageclimatisation,essencegaz

但是我不知道如何使用复选框获得此结果。

function rclr_query_string($q)
{
    foreach (get_taxonomies(array(), 'objects') as $taxonomy => $t) {
        if ('post_tag' == $taxonomy) {
            continue;   // Handled further down in the $q['tag'] block
        }
        if ($t->query_var && !empty($q[$t->query_var])) {
            if (is_array($q[$t->query_var])) {
                $q[$t->query_var] = implode(',', $q[$t->query_var]);
            }
        }
    }
    return $q;
}
add_filter('request', 'rclr_query_string', 1);

最新更新