I am trying to find a way to add &#filterTop
to the end of the URL if ccm_order_by_direction=desc
is in the URL.
I assume JavaScript would be the easiest, but if there is a PHP way that would work too.
The catch is that it would have to update the existing page link. The links are created in the backend and I can not edit them directly. I know I did something like this before, I can not remember how or where the info would be.
I got this far. I just don’t know where to go next.
$(document).ready(function() {
if (window.location.href.indexOf('ccm_order_by_direction=desc') !== -1) {
let url = '<?= $actual_link ?>';
url = url.replace('ccm_order_by_direction=desc', 'ccm_order_by_direction=desc&#filterTop');
var link = document.getElementsByClassName('.page-link');
link.href = url;
}
});
The links that needs updated look like this.
<li class="page-item">
<a class="page-link" href="/switches-damper-arm-tilt-tip-over-limit?ccm_paging_p=2&ccm_order_by=popular&ccm_order_by_direction=desc">2</a>
</li>
I would like them to look like.
<li class="page-item">
<a class="page-link" href="/switches-damper-arm-tilt-tip-over-limit?ccm_paging_p=2&ccm_order_by=popular&ccm_order_by_direction=desc&#filterTop">2</a>
</li>
2
Answers
Without troubleshooting all your code, I can see right off the bat this line won’t work:
For that, you’d need to iterate – in which case I recommend using querySelectorAll (which will return an iterable collection list) which you can then manipulate in a forEach loop – and change the links one by one, but you’re using jQ so you can simply do:
Which (considerately) will take care of it. However, I wanted to explore how to do this without string manipulation, so I have a solution here that uses URLSearchParams to objectify the query string and then allow you to use the baked in methods to replace the item in question. I use decodeURIComponent at the end since the URLSearchParams will decode the symbols (? #) into %d4 or whatever.