How to Show Links Only on Home Pages of WordPress Blogs?


Sometimes you just want to show some links on the home page of blog but not other pages, then the following trick is useful.

First of all, you can decide where to put the links, either using WordPress widgets (Appearance -> Wigets -> Text/HTML) or you can edit the template file footer.php.

Then, by using the Javascript code presented below, we can check the current URL and decide whether to print the hyperlinks or not.

We can use document.URL or windows.location to get the current URL of the page. You can check if the length of the URL is the minimal for example, https://helloacm.com/ (21 characters) or //helloacm.com/ (20 characters). So if the length of the URL is less than 20 characters, then it is at the home page.

1
2
3
4
5
6
<script>
var cururl = window.location.toString();
if (cururl.length <= 20) {
  document.write("//helloacm.com");
}
</script>
<script>
var cururl = window.location.toString();
if (cururl.length <= 20) {
  document.write("//helloacm.com");
}
</script>

Alternatively, you can make it easy to read.

1
2
3
4
5
6
<script>
var cururl = window.location.toString().toUpperCase();
if ((cururl == 'HTTP://HELLOACM.COM/') || (cururl == 'HTTPS://HELLOACM.COM/')) {
  document.write("//helloacm.com");
}
</script>
<script>
var cururl = window.location.toString().toUpperCase();
if ((cururl == 'HTTP://HELLOACM.COM/') || (cururl == 'HTTPS://HELLOACM.COM/')) {
  document.write("//helloacm.com");
}
</script>

Now, the link will only be printed when it is at home page. Most modern search engine bots will know how to crawel the Javascript code, so they will follow the links (that are generated by Javascript) without problems. So don’t let SEO hold you back from doing this.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
305 words
Last Post: Performance Testing the HDD/SSD/NVme Hard Disk speed on VPS/Dedicated Server
Next Post: How to Disable [Power, Sleep, Wakeup] Buttons at Keyboard on Windows?

The Permanent URL is: How to Show Links Only on Home Pages of WordPress Blogs?

Leave a Reply