admin管理员组文章数量:1126140
I am using the_posts_pagination()
on my native archive pages and paginate_links()
on some pages with custom queries.
However, the function seems to pass on any url parameters from the current page. If I add ?xyz=123
manually in the url bar of my page, it will be passed on to the links in the pagination (it becomes permalink/page/d/?xyz=123
). I'd like to have control of my output and only have it to show current permalink + page/d/
.
On my archive pages I use the_posts_pagination()
plain with no parameters, so this seems to be the default behaviour but I can't find any argument that turns it off?
I am using the_posts_pagination()
on my native archive pages and paginate_links()
on some pages with custom queries.
However, the function seems to pass on any url parameters from the current page. If I add ?xyz=123
manually in the url bar of my page, it will be passed on to the links in the pagination (it becomes permalink/page/d/?xyz=123
). I'd like to have control of my output and only have it to show current permalink + page/d/
.
On my archive pages I use the_posts_pagination()
plain with no parameters, so this seems to be the default behaviour but I can't find any argument that turns it off?
1 Answer
Reset to default 0To address your concern about controlling the URL parameters in pagination links in WordPress, it's true that both the_posts_pagination() and paginate_links() functions can inherit existing query parameters from the current URL. This behavior is designed to maintain the context of the current query, including any filters or search terms, across paginated pages.
However, if you want to exclude certain query parameters or limit the parameters to only those that you specify, you can achieve this by filtering the generated links. WordPress provides a way to modify the pagination links through filters.
Here's a basic approach to filter out unwanted query parameters:
Use the paginate_links filter to modify the pagination links. Parse the URL and remove or manipulate the query parameters as needed. Here's an example of how you might implement this:
function wpb_filter_pagination_links($link) {
// Parse the URL
$url_parts = parse_url($link);
// Parse the query string
if (isset($url_parts['query'])) {
parse_str($url_parts['query'], $query_args);
// Remove unwanted query args
unset($query_args['xyz']); // Replace 'xyz' with the parameter you want to exclude
// Rebuild the query string without the unwanted parameter
$url_parts['query'] = http_build_query($query_args);
// Rebuild the URL
$link = http_build_url($url_parts);
}
return $link;
}
add_filter('paginate_links', 'wpb_filter_pagination_links');
This code will remove the query parameter xyz from your pagination links. You can modify the unset() line to remove other parameters as needed.
Note that this approach requires careful consideration of how it might affect the functionality of your site. For example, removing query parameters related to filtering or searching could impact the user's navigation experience.
As always, test your changes in a development environment before applying them to your live site to ensure that they work as expected and do not introduce any issues.
本文标签:
版权声明:本文标题:pagination - Stop the_posts_pagination() from passing on any url parameters? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736665144a1946622.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_the_posts_pagination
, which itself usespaginate_links
internally. Note that what you're asking for will cripple ugly permalinks, and may also break some archives, e.g. pagination on searches would be broken by this because the search term would be stripped out when going to page 2. What is the URL parameter you're trying to strip out? Or is this a theoretical concern you're trying to pre-empt? – Tom J Nowell ♦ Commented Jan 25, 2024 at 8:58paginate_links
, and that function returns an array, it's a different function that then creates the links from it. The URL args are in those arrays – Tom J Nowell ♦ Commented Jan 25, 2024 at 11:19