Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Controlling post excerpts

From: Dreamweaver and WordPress: Building Sites

Video: Controlling post excerpts

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.

Controlling post excerpts

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.

Show transcript

This video is part of

Image for Dreamweaver and WordPress: Building Sites
Dreamweaver and WordPress: Building Sites

21 video lessons · 16412 viewers

Joseph Lowery
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Dreamweaver and WordPress: Building Sites.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.