Build Your Own Business Website header image

Introduction to Customizing Thesis 2.1 Using PHP – Part 11 – Example #4 – Create a Custom Excerpt Box

Difficulty Level -

Filed Under Topics - ,

Listed Under Lesson Subjects -

Applies to -

Whoops, you've found some premium content!

Watch the opening clip of this video to preview it,
the full video is available to paid members.

Now that we’ve created a basic Hello World box let’s move on to example #4 where we’re going to create a custom excerpt box.

Create a New Box Based On an Existing One

We’re going to take this box, this is how I do it, and I create a copy of it. I’ll change the copy so instead of hello-world-1 it’s going to be custom-excerpt. Now let’s open up box.php and we just change name and description to Custom Excerpt and “This box gives the user the opportunity to specify the desired length of the excerpt and a custom “Read More” link.”.

Add Functions

Then custom_excerpt, custom_excerpt, still $type = ‘box’. In this case, I’m going to make name and title the same, so BYOB Custom Excerpt. Then we’re going to come back and look at what we did here in master.php.

Remember that we had this custom_excerpt _length? Well, we’re actually going to copy this function, come over to our box and then paste that function inside the box. Now, we want to give a public declaration to it so public function byob_custom_excerpt_length.

Functions and Classes

Since it’s inside this class, it doesn’t need to be unique. There can’t be two functions in the same class and the same name but there can be as many functions in the world in the site as you want as long as they are each in different classes. So custom_excerpt_length or in fact, we’re just going to take it down to excerpt_length.

Excerpt Length

In this case, we’re not going to use the conditional language. We’re just going to do that. So this is going to return 20 characters where this is used and then instead of echo ‘Hello World’, we’re going to use that function echo get_the_excerpt. So this is now going to echo the excerpt any place we put that. And here’s our filter for the length of the excerpt.

Box Only Affects Specific Excerpt – Hooking the Filter

And the only thing we’re missing here is hooking the filter and we’re going to hook that filter up here inside of the HTML. That means that this filter will only fire when this HTML fires which is how we make it so that the excerpt only affects the specific excerpt in question and not all excerpts.

So we’ll come back over here to master.php and we’re going to look at this add_filter right here. We’re going to copy that, come back over here to box.php. Then directly under that inside of this, we’ll add the filter.

Change the Syntax of the Function

We’re not going to use the priority because it doesn’t matter and we’ve changed the name to this. And there’s one other thing to do and that is when you are referencing a function that is inside of your class with an add_filter or an add_action statement, you actually have to change the syntax up a little bit.

The way this syntax has to be here is array and then inside that array it’s a $this, and then the closing parenthesis after that. So add_filter ‘excerpt_length’, and then this is just the format.

You don’t have to know why but if you’re referring to a function that is inside of your excerpt, you are either adding the action or adding the filter of that function, then you’d use the name of the action or the name of the filter and then array($this, and then the name of the function.

This little bit of language means that you are adding this instance of excerpt link and this is the function excerpt_length so you are adding this instance of excerpt length to the excerpt_length.

Again, this is not something you need to understand just as long as whenever you write an add_action or an add_filter for a function that is inside of your class then instead of the function name alone, you put it inside of this array format, okay?

Now if we save this and upload it…wait, we haven’t uploaded it yet. I need to upload the custom-excerpt box, we haven’t done that yet. So we upload the whole custom-excerpt box. We come over to Thesis home to activate the box. So load custom-excerpt, save it and then we come back over to our Skin Editor and now we have access to this BYOB Custom Excerpt.

The BYOB Custom Excerpt

Now, I could take this excerpt out of here, out of the Main Loop, and then create this excerpt. That’s BYOB Custom Excerpt. We’re going to call this Home Excerpt. Say okay to that. And then instead of using the Thesis Excerpt, we’re just going to shift+drag this down into the Home Post Box and then down into that position. Save this template and refresh it. Now you can see we’ve got the shorter excerpt.

You know, I don’t think I uploaded master.php after I changed it. Let’s go look at the Home page for a second. So we’ve got the full excerpt blank showing up over here but if we go over to the News page, well it’s not exactly working the way I expected it to work and give me just a second to check my code.

No, that’s exactly the way I wrote this code. You know, it’s interesting. Let’s go ahead and put the excerpt back in here and let’s take this Home Excerpt and put it in the sidebar query box instead. Drag this out and drag that excerpt down and let’s save the template.

Actually interestingly, I didn’t see that coming because I didn’t actually test it that way. If it’s sitting inside of the Main Loop, it’s affecting everything on that page. If it’s sitting inside of a Secondary Loop, it’s only affecting what’s in that Secondary Loop and not the rest of the page.

So for example here, we’re down to 20 but here we’re back to the normal 55 when I moved that excerpt over to here. Now, as a practical matter, that may not matter.

0 Comments… add one
0 comments… add one

Leave a Comment