Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In many cases, you want to restrict certain content or behaviors to a particular page or post in your site. This is relatively easy when it comes to pages or posts, because you're dealing with single items. But what do you do from the page in question is an index page. We can't test for a specific post id, because the post keeps changing. Instead, we have test to see if the currently displayed template is the one we want and that it is in the order we want it to be in. Let me explain what I am talking about here.
We added a welcome message to our index template. It displays at the very top of the index template and then underneath you have the index content. However, if I scroll down, and I jump to Page 2 of the index, the welcome message still appears. Logically, we only want the welcome message to appear on the front page, so Page number 1 of the index template. So how do we do that? Well, we have to ask WordPress several questions. We both have to ask it, is this the front page that you are already currently displaying.
And we also have to ask it, is this first of the index pages, so that if it's the 2nd or 3rd or 50th of the index pages, it doesn't display this welcome message. To do this, we are going to use two conditional tags. The first one--and most important one--is one that you might end up using a lot. It's one that's called is_front_page. It basically asks if the page you're currently looking at is the front page, whether or not that page be an index page or if it's a static page.
This depends on how you set what page is displayed as a front page under Settings > Reading, and a FrontPage displays. We are going to use is_front_page in conjunction with another conditional statement called is_paged and I'll explain what that one does in a second. To restrict the welcome message to only the front page, what I am going to do is go to index.php, where this template part is being called, and then I'll wrap the call for this template parts in a conditional statement.
So I'll say if is_front_page, because I want to see that it is the front_page and then I'll also want to test to see if this is the second or third or fourth page. So what I am going to do is use a function called is_paged. is_paged basically tests to see if this is the second, or third, or fourth, or fifth page of an index. But in this case, I want to see that this is not a paged item, meaning it's not paged 2, or 3, or 4, so I am going to put an exclamation point in front of it say, and it is_not_paged.
So if both these conditions are met, I want get_template_part (welcome) to run then I am going to end my conditional statements, save index.php, and then reload this page. Notice that this is currently Page 2 of the index. And now you see the welcome message disappeared from the top, but if I jump back to the front page, the welcome message appears. Just to make sure, I am going to scroll to the bottom and jump to a different page, so I'll jump to Page number 3, and just as it was, Page number 2 we don't see the welcome message.
Here you can see how using conditional statements wisely can give you very interesting results and very detailed control. I'll show you another example of a site I used the same type functionality on to make something happen. On blendinsider.com, I created a function that displays the first story on the front page with larger fonts and more information than the second and third and so on. I'm using the exact same conditional statement is_front_page and is_paged to make sure that it only displays on the first page in the index.
I'm also using an additional conditional statement to test that this only appears on the first post on the index page. So this should give you an idea of how far you can take it when you use these conditional statements. So just remember, anytime you want to display something in just one place, ask yourself if you can ask the database a question and then make that computer and the database figure out what to display using conditional statements.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.