【发布时间】:2019-06-21 19:26:33
【问题描述】:
当我尝试按类别过滤自定义帖子类型存档时遇到问题。
我想按多个类别过滤 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>
当我选中一个或多个复选框并提交表单时,我会得到这个 URL
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
但我不知道如何使用复选框获得此结果。
【问题讨论】:
-
表单提交的 PHP 处理是什么样的?
-
它应该被 WordPress 自动理解,所以我应该无事可做......就像我发现的一个网站的 URL:
https://popsciences.universite-lyon.fr/agenda/?ajax=1&recherche=&type-evenement%5B%5D=animation&type-evenement%5B%5D=ateliers&type-evenement%5B%5D=balade-urbaine&lieu=0&limit_down=&limit_up=,至少它不应该破坏结果,因为它只是查询参数,所以如果我没有在 php 中对它做任何事情,页面应该正确显示。 -
请给我们孔码