Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When a client frequently updates the blog, sometimes it's better to show just a portion of several posts with an option to read the full post just a click away rather than the WordPress default, which is to show several posts in their entirety. In WordPress, these post portions are known as excerpts. Setting up your blog page to show excerpts rather than full posts is actually dirt simple, but getting WordPress to display your excerpts the way you want them to, that takes a little bit more effort.
However, you can handle all the coding right in Dreamweaver. First, let's take a look at the default behavior. I have opened my site root index.php file here in Live view. If we scroll down, we see we've got one big post. Now, there's just one post here, and that's because in the previous video we used the query_posts function, which you see here on line 5, to limit the post just to 1. I'm going to expand that to 3, save the file, and now let's go back to index.php and refresh Live view.
Now, when I scroll down the page, I have a number of posts. Now, I only have a couple of posts in here, so my limit of three is not reached. Now let's go back to the index.php. That's located in the Themes folder. You maybe wondering if you've got a number of different index.php files opened, how do you tell the difference from them? Well, on the coding toolbar, there is the Open Documents button. If you click that, you'll see a list of all your opened documents with complete path names and the check mark next to the one that's currently being displayed.
It's a really handy feature and sometimes when I have multiple index pages opened, which you will, in WordPress, this is invaluable. Okay. So as I said, we are in the themes index right here. Now, let's switch the blog page from full post to excerpt. As I said, it couldn't be simpler. Here in the center of the page, in the div with a class of entry, we have the PHP function the_content. I'm going to change that to the_ excerpt, and save it, and voila! as the French will say. Let's go back to our index.php from the site root, refresh, and now we see just excerpts of the files.
Now, this is the default presentation for excerpts. You will notice that there is a limit of a specific number of words. It's 55, in case you are wondering. And it displays an ellipsis in the square brackets at the end. Now, you might think that the ellipsis would be clickable, but it's not. To see the full post, you have to go up and click on the title. You should also realize that there's something else that's happening that may not be immediately obvious, but one result of using the excerpt function is that all HTML tags are stripped from the text.
Now, let's see how we can customize the excerpt in a couple of ways. We will start by modifying the links of the excerpt. To do that, we will need to open up the function.php page that's found within our theme folder. I'll open it and then go into code. Now I already have one custom function here that registers the sidebar, so we are going to add another one. Make sure you position your cursor right within the same PHP code block. And now to help me do this, I'm going to go and open up a file from my exercise files that is called functions_code.php, and that is found in the Chapter 2, 02_02 folder.
So let's open up the functions code there, and we are going to select function new_excerpt_length. Make sure you get the line that says add_filter if you're working from the exercise files. I'm going to copy that, switch back over to functions.php, and paste it in. Now, what this code is doing is it's setting up a new function called new_excerpt_length with an argument variable of length and it's returning the value of 7. That's the number of words that will be used for the excerpt. Let's try it out.
So I'm going to save functions.php, go back to my index.php file, press Refresh, and there is my seven words in each excerpt. All right! Seven might be just a little bit too short, so let's head back to functions.php and change that 7 to let's say 40. Okay, so we will save that value and back to my index.php, refresh Design view, and I am not going to count them, but it looks like forty words.
So the other thing we might want to change is this unclickable ellipsis here. Let's add a custom trailing phrase to replace the easily overlooked ellipses that we see here, and at the same time, make that linkable. Let's head to functions_code.php and pick up this top function along with the add_filter line, copy that, and move to our functions.php file. And then I'll add a new line return, and paste it in, select, and tab over.
What this code is doing is creating a function called new_excerpt_more. It pulls in a global variable, post, and then it returns what will appear at the end of the excerpt. And what will appear at the end of the excerpt now is an ellipsis followed by a space, followed by an anchor tag with a target of the permalink of the post's ID, so you can click on Read more, which is the text you see within the anchor tag and it will go to that post. We will cover how add_filters work in a later video, but here you can see that we have the add_filter function that is filtering the default function excerpt_more with our new function new_excerpt_more.
Again, save, head back to index.php and refresh Design view. So let's scroll down, and there is our new Read More link. I'm going to go ahead and press Ctrl and then click on it to follow the links. We go to our full-page post. This same type of customization is applied wherever excerpts are used, as on the search results page.
Get unlimited access to all courses for just $25/month.Become a member
58 Video lessons · 65522 Viewers
61 Video lessons · 96308 Viewers
56 Video lessons · 110069 Viewers
82 Video lessons · 105059 Viewers
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.