WordPress Tips – How To Show Content Only On The Home Page
This is a WordPress tip on how to show content exclusively on the home page. Some times you may want to show specific content – such as links to web resources, or text, only on the home page (front page) of your site. Here is how you do it.
WordPress has got an inbuilt PHP function to show content only on the home page of your site. This function is called is_home().
And the actual code you have to use for displaying specific content only on the home page is as follows.
<?php if(is_home() && $post==$posts[0] && !is_paged()) { ?>
//You can add your content to be shown on the home page here.
//This includes images, links, and text.
<?php } ?>
Where to insert this code ?
That depends on where you want to see the content on your home page.
The default WordPress index or home page comprises of four sections namely -
- the Header,
- the Body,
- the Sidebar, and
- the Footer.
The corresponding files that insert the respective sections are header.php, sidebar.php, footer.php, and of course index.php.
So if you want specific content to be seen somewhere on the sidebar of your website – but only on the home page, then you insert the code in the sidebar.php file in your WordPress template.
But if the content is to be seen in the Header – exclusively on the home page, then you have to insert the code in the header.php and so on.
Exec-PHP WordPress Module
If you are a non-techie person and do not wish to dirty your hand editing template code, then this WordPress module is for you.
By default, WordPress doesn’t execute any PHP code when you insert it in your content editor. Exec-PHP is a module that allows you to do just that. By installing Exec-PHP module, you will be able to insert and execute PHP code in your content and Widgets as well.
So if you have installed and enabled the Exec-PHP module for your WordPress website, then you can insert the code I mentioned above in one of your template Widgets.