-5

So in Wordpress I have a static link in the footer that appears in all pages and I would like the URL in the link to change when but only when it's in a certain page. So it's like

All pages - footer link goes to href="https://website-A"

Except when on page 'x'(or lets say the About Page) then footer link goes to href="https://website-B"

Is there a way to do that in jQuery or JS?

Thanks,

Nsokyi
  • 394
  • 4
  • 21

2 Answers2

1

Try something along the lines of this...

$(document).ready(function() {
    var url = window.location.href;
    var UrlofpageX = url.indexOf('theurlyouwanttolookfor');
    if (UrlofpageX >= 0) {          
        $('.yourlink').append('<a href="https://website-B"><li>Your different link</li></a>');         
    }
    else {
          $('.yourlink').append('<a href="https://website-A"><li>Your original link</li></a>');  
    }
});

So what happens here is you get the URL of the page that you're currently on. It gets stored in a variable. You then look for the words within that URL that will determine that you are on this particular page X and not some other page.

Then you run an If/else. IF the variable has something in it after the check then you know you're on page X and you append a new link. ELSE you're on a normal page and you set the regular link.

0

You could use JavaScript in order to achieve that, using the window.location.href in order to get the current link of the page and then changing the text depending on the page. BUT, this is really an awful solution for a LOT OF REASONS.

You should use directly Wordpress in order to do that and use PHP. There are really a lot of ways for doing that. You can implement your own widget/plugin, you can create a text plugin on the widget pages (using also a wordpress plugin in order to embed PHP code there) or you can directly add this PHP code inside the template section where to show the link, retrieve the current page and display the wanted text. I suggest you also to change the tags to PHP and Wordpress, because as I said above, performing this task with JavaScript it's the worst solution you can do.

quirimmo
  • 9,800
  • 3
  • 30
  • 45