WooCommerce paginate_links使用add_args时URL数组索引及多参数丢失问题
WooCommerce 归档页多同名URL参数分页丢失问题解决记录
我为WooCommerce归档页面开发了AJAX加载的自定义筛选器,其余功能正常,但分页存在问题:页面刷新后,当URL包含多个同名参数(如 example.com/category/filter_fabric=cotton&filter_fabric=polyester)时,分页链接仅保留最后一个参数。
尝试过程与中间问题
初始修改解决参数保留,但URL出现数组索引
修改woocommerce/loop/pagination.php文件,添加代码获取URL参数并传入分页函数://Get parameters from URL $url_parameters = $_SERVER['QUERY_STRING']; $paginate_add_args = proper_parse_str($url_parameters); //@TODO: the "&" that are supposed to come from array within array/nested array, i.e. filter_fabric are becoming "%5B0%5D" instead of "&"将
$paginate_add_args传入paginate_links的add_args参数后,多参数保留的问题解决了,但URL中出现了数组索引(如example.com/category/page/2/filter_fabric%5B0%5D=cotton&filter_fabric%5B1%5D=polyester),不符合预期格式。调整URL格式但参数仍丢失
通过array_walk将数组转为字符串,让分页链接显示为example.com/category/page/2/?filter_fabric=cotton&filter_fabric=polyester,但点击链接后页面依然只保留最后一个参数。
最终解决方案
添加以下代码禁用WordPress的规范重定向,彻底解决了多同名参数在分页时的丢失问题:
remove_action('template_redirect', 'redirect_canonical')
内容的提问来源于stack exchange,提问作者renjogabro
相关产品推荐
相关产品推荐

