Индекс массива распечатывается при использовании add_args в WooCommerce/Wordpress paginate_links() ⇐ Php
-
Anonymous
Индекс массива распечатывается при использовании add_args в WooCommerce/Wordpress paginate_links()
I have been trying to scour the internet for the answer, but I didn't find it. I have created a custom filter for WooCommerce archive pages, which is being loaded by ajax. All works fine, except for pagination.
When refreshing the page and having several parameters with same name (e.g. example.com/category/filter_fabric=cotton&filter_fabric=polyester), the pagination links only take into account the last parameter (in this case, polyester). I was able to get past this issue by going into loop/pagination.php in woocommerce and adding the following:
`//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 "&"` Then I passed that argument in the 'add_args' of paginate_links() (also in loop/pagination.php) as following:
` ` This solved the issue in that all instances of my parameter are being loaded in the link, but the problem is that since my filter_fabric parameter becomes an array, it prints out the array index in the URL, as so:
example.com/category/page/2/filter_fabric%5B0%5D=cotton&filter_fabric%5B1%5D=polyester
Does anyone have any idea of how to solve this issue? Anything would help greatly!
I have tried using the methods mentioned above.
UPDATE/EDIT #1 So I think I sort of managed to fix the pagination link by doing this method:
//Get parameters from URL $url_parameters = $_SERVER['QUERY_STRING']; $paginate_add_args = proper_parse_str($url_parameters); # Function that turns the array items into string, like this # e.g. array(filter_fabric => array(cotton, polyester, linen)) # into: # array(filter_fabric => 'cotton&filter_fabric=polyester&filter_fabric=linen') $walk = function(&$item, $key) { if (is_array($item)) { $item = implode('&' . $key . '=', $item); } }; array_walk($paginate_add_args, $walk); //var_dump($array); //Outputs: 'cotton&filter_fabric=polyester' This actually renders the pagination links correctly, with the link being:
'example.com/category/page/2/?filter_fabric=cotton&filter_fabric=polyester' But...
When I actually click that link, the page loads with only the last parameter. It almost seems as if it redirects with only the last parameter, that is, into this:
'example.com/category/page/2/?filter_fabric=polyester' Anyone have any idea why this happens?
UPDATE/EDIT #2 I think I managed to solve this issue by adding a remove_action('template_redirect', 'redirect_canonical') Please see the answer in this link: Adding multiple values to a query variable in WordPress
Источник: https://stackoverflow.com/questions/753 ... ess-pagina
I have been trying to scour the internet for the answer, but I didn't find it. I have created a custom filter for WooCommerce archive pages, which is being loaded by ajax. All works fine, except for pagination.
When refreshing the page and having several parameters with same name (e.g. example.com/category/filter_fabric=cotton&filter_fabric=polyester), the pagination links only take into account the last parameter (in this case, polyester). I was able to get past this issue by going into loop/pagination.php in woocommerce and adding the following:
`//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 "&"` Then I passed that argument in the 'add_args' of paginate_links() (also in loop/pagination.php) as following:
` ` This solved the issue in that all instances of my parameter are being loaded in the link, but the problem is that since my filter_fabric parameter becomes an array, it prints out the array index in the URL, as so:
example.com/category/page/2/filter_fabric%5B0%5D=cotton&filter_fabric%5B1%5D=polyester
Does anyone have any idea of how to solve this issue? Anything would help greatly!
I have tried using the methods mentioned above.
UPDATE/EDIT #1 So I think I sort of managed to fix the pagination link by doing this method:
//Get parameters from URL $url_parameters = $_SERVER['QUERY_STRING']; $paginate_add_args = proper_parse_str($url_parameters); # Function that turns the array items into string, like this # e.g. array(filter_fabric => array(cotton, polyester, linen)) # into: # array(filter_fabric => 'cotton&filter_fabric=polyester&filter_fabric=linen') $walk = function(&$item, $key) { if (is_array($item)) { $item = implode('&' . $key . '=', $item); } }; array_walk($paginate_add_args, $walk); //var_dump($array); //Outputs: 'cotton&filter_fabric=polyester' This actually renders the pagination links correctly, with the link being:
'example.com/category/page/2/?filter_fabric=cotton&filter_fabric=polyester' But...
When I actually click that link, the page loads with only the last parameter. It almost seems as if it redirects with only the last parameter, that is, into this:
'example.com/category/page/2/?filter_fabric=polyester' Anyone have any idea why this happens?
UPDATE/EDIT #2 I think I managed to solve this issue by adding a remove_action('template_redirect', 'redirect_canonical') Please see the answer in this link: Adding multiple values to a query variable in WordPress
Источник: https://stackoverflow.com/questions/753 ... ess-pagina
Мобильная версия