Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When I design and develop WordPress themes I try to imagine what the ideal experience for someone who stumbles upon the site for the first time. What I've found is, often people hear about a post on a site, and they go to the front page to read that post. And if they land on the front page like this, and see that the first post has truncated and only showing the excerpt and see that the post blends in with every other post, they're less likely to read it. To deal with this problem, I like to display the first post on the front page differently from the rest of the posts in the archives.
So I display the full-featured image, and the full content and I give the post author the ability to truncate the content wherever she wants, or she can put in the more tag anywhere within the content. And only there will the continue reading link appear. This can all be achieved using some conditional statements, and by coping code over from content single dot php, and inter content php. Since all this will happen in the content on the front page, it's all going to happen inside content dot php. So I'll open that template. And then first let me just show you how you can target just the first post.
On the front page, and not any of the other pages. So I'll create a new function here, I'll put in a PHP delimiter, and close it. And then I'll create a conditional statement if. And here we're testing for several different things. First we want to get the first item, and only the first item. And we do that by getting the query that was sent back to us from the database. And then we're saying we only want the current posts, and we're checking for the number. So I'll say Dollar, WP, query. So this is the item that was sent back to us from the database, and I'm looking inside the item for the current posts.
And I'm checking to make sure that the current post is item number zero. So in a database query, item number zero is the first item. Then I also want to check that this is not a paged template. Meaning no one has gone down to the bottom and clicked on page two, or three, or four. So I'll do that by using the is paged function. So I'll say not is paged. So finally I need to know that this is the index template, so I'm not applying this highlighting to an archive template. And the way I do that is by using the function is front page.
And now, if all of these conditions are met, I want to display the text, This is the first post on the front page! Now, I can save my function and go and reload my front page. And we see the text, This is the first post on the front page, on the first post of the front page, not on the second one, or any of the other ones. And, if I navigate down, and switch to page number two. You'll see the text doesn't appear here, and it also doesn't appear if I go to one of the archives.
So now I know my conditional statement works, and this is where we need to make some dramatic changes to the content.php template. The first I'll take out this example I put in, and then let's just think quickly about what we want to do here. First we want to scroll down to where the content is displayed, and here we want to make a condition where we display the full content on the first post, and then only the excerpt on every other post. In my code snippets, I have the code for that, and you can see it's fairly extensive. It's this first bulk of code here.
So I'm going to replace entry content, and entry footer here. So I'll grab all of that and replace it, and what this new bulk of code does is it runs our conditional test to make sure that this is our first post. If it is the first post it displays the content so the full content. And then it sets up the link at the bottom the one that says continue reading, and gets it to say read the article because here we have to assume the entire article has been displayed. Then if the condition is not met, meaning it's any of the other archived pages, or this is not the first post, we display the excerpt with the old read more link.
I'll save content.php, and test this to make sure it works. And now you'll see my first post displays all the content and it says read the article at the bottom. And then the next posts displayed the truncated excerpts and say Continue Reading. Now we have to deal with the featured image. So in my current content.php template, we're displaying the featured image inside the index box. But, for my first post, I want to display the larger featured image outside of the index box and then.
Not display the feature image also inside it. So here I need to create a more complicated conditional statement. So I have the whole thing here in the second bulk of code and this time I'm going to replace everything from div class index box down to entry header. Looking at the code, you'll see exactly what happens here. First, we'll run the conditional test, and if we have the first pulse on the index page, we display the full size large thumb. And we wrap it in an anchor, and we also give it the new class front index template.
And if we have this condition, and then we go down and we display the div class index box. And we'll also add the additional has thumbnail class, and you'll see why in a second. If on the other hand, this is any one of the other pages, or if it's not the first post, then we just put a date of the class and box followed by the post thumbnail. I'll save page content again. and reload my page and now you see with display the full page display at the top followed by the full article.
In then the next pulse just have the regular featured images. Now we just have to apply a tiny little bit of styling to make sure that this displays properly. Because right now you see my featured image appears separate from the rest of the post, and I actually want to shift the post in, on top of the featured image. And that's why I applied this extra has thumbnail class to they index box div, and also why I changed the div for the featured image to front index thumbnail. If you go to the code snippets.
And you scroll down you'll see here under style.css, we have some custom styles just for this situation. So I'll copy this out and go to style.css, and again this relates to my featured images so I'm going to go down to my media section, twelve point oh, scroll past all the stuff until I get to my featured images. Here they are. And here I'm going to paste in this featured image above small index thumbnail. And these new styles just shift the content around a little bit so that we can get the content for the actual polls to appear slightly on top of the featured image, so that their not dislocated from each other.
I'll save the style sheet a final time, and reload the page. And now you see the post content shifted up, a little bit on top of the featured image to show that they're closely related to each other. I can click on the featured image to go to the post, and when I scroll down I see I have the full content. So now by simply adding in a little bit of extra code and some conditional statements, I've created a very custom experience just for the front page of my site. And all the code I used here was used previously in the course in different places.
I just simply moved it around and copy it from one template into the other.
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.