在 WooCommerce/Wordpress paginate_links() 中使用“add_args”时会打印数组索引

问题描述 投票:0回答:1

我一直在网上寻找答案,但没有找到。 我为 WooCommerce 存档页面创建了一个自定义过滤器,该过滤器由 ajax 加载。除了分页之外,一切正常。

刷新页面并具有多个同名参数时(例如 example.com/category/filter_fabric=cotton&filter_fabric=polyester),分页链接仅考虑最后一个参数(在本例中为聚酯)。我通过进入 woocommerce 中的loop/pagination.php 并添加以下内容解决了这个问题:

`//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_links() 的“add_args”中传递该参数(也在loop/pagination.php中),如下所示:

`<nav class="woocommerce-pagination">
    <?php
        echo paginate_links( apply_filters( 'woocommerce_pagination_args', array( // WPCS: XSS ok.
            'base'         => $base,
            'format'       => $format,
            'add_args'     => $paginate_add_args,
            'current'      => max( 1, $current ),
            'total'        => $total,
            'prev_text'    => '<i class="fa fa-angle-left"></i>',
            'next_text'    => '<i class="fa fa-angle-right"></i>',
            'type'         => 'list',
            'end_size'     => 2,
            'mid_size'     => 1,
        ) ) );
    ?>
</nav>
`

这解决了我的参数的所有实例都被加载到链接中的问题,但问题是,由于我的filter_fabric参数变成了一个数组,它会打印出URL中的数组索引,如下所示:

example.com/category/page/2/filter_fabric%5B0%5D=棉&filter_fabric%5B1%5D=聚酯纤维

有人知道如何解决这个问题吗? 任何事情都会有很大帮助!

我已经尝试过使用上述方法。

更新/编辑#1 所以我想我通过这种方法设法修复了分页链接:

//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'

这实际上正确呈现了分页链接,链接为:

'example.com/category/page/2/?filter_fabric=cotton&filter_fabric=polyester'

但是...

当我实际单击该链接时,页面仅加载最后一个参数。看起来好像它只使用最后一个参数进行重定向,即重定向到此:

'example.com/category/page/2/?filter_fabric=polyester'

有人知道为什么会发生这种情况吗?

更新/编辑#2 我想我通过添加

remove_action('template_redirect', 'redirect_canonical')
成功解决了这个问题 请参阅此链接中的答案: 在 WordPress 中向查询变量添加多个值

php wordpress woocommerce filter pagination
1个回答
0
投票

您可以修改 paginate_links() 函数的输出,通过在将参数添加到分页链接之前对参数进行编码来删除数组索引。您可以使用 http_build_query() 函数来执行此操作。方法如下:

<nav class="woocommerce-pagination">
<?php
    $paginate_add_args = http_build_query($paginate_add_args);
    echo paginate_links( apply_filters( 'woocommerce_pagination_args', array( // WPCS: XSS ok.
        'base'         => $base,
        'format'       => $format,
        'add_args'     => $paginate_add_args,
        'current'      => max( 1, $current ),
        'total'        => $total,
        'prev_text'    => '<i class="fa fa-angle-left"></i>',
        'next_text'    => '<i class="fa fa-angle-right"></i>',
        'type'         => 'list',
        'end_size'     => 2,
        'mid_size'     => 1,
    ) ) );
?>

这将以查询字符串格式对 URL 参数进行编码,因此不会在 URL 中显示数组索引。

© www.soinside.com 2019 - 2024. All rights reserved.