Start learning with our library of video tutorials taught by experts. Get started

Dreamweaver and WordPress: Building Sites

Controlling post excerpts


From:

Dreamweaver and WordPress: Building Sites

with Joseph Lowery

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.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
Dreamweaver and WordPress: Building Sites
1h 42m Intermediate Feb 24, 2012

Viewers: in countries Watching now:

In this course, author Joseph Lowery shows how to create a robust WordPress-based site using Dreamweaver. The course lays out the essentials of planning a site, explains how to implement custom sidebars, and demonstrates building page templates. The course also explains how to integrate PHP functionality and extend the WordPress database.

Topics include:
  • Understanding development environment requirements
  • Working with existing WordPress content
  • Handling standard pages
  • Managing full posts
  • Coding a basic template
  • Adding a custom sidebar
  • Working with WordPress functions
  • Inserting PHP code in posts
  • Including a recordset
  • Outputting data
  • Linking to dynamic pages
Subjects:
Web CMS
Software:
Dreamweaver WordPress
Author:
Joseph Lowery

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.

There are currently no FAQs about Dreamweaver and WordPress: Building Sites.

Share a link to this course
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.
Upgrade now


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.

Upgrade now

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
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



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.

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