Build Your Own Business Website header image

How to Use Post Formats in Thesis – Part 3 – Using the Thesis Custom Loop API

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.

In part 3 of this series on Using Post Formats in Thesis, we talk about and show what the Custom Loop API is and then we show how to add it to the post format. The custom loop activates the effect that you want for a particular post format.

We create a new Custom Loop Class that will extend the Thesis Custom Loop on a Single Post and then add conditional arguments. For example if the post has a format of “aside”, it will display the content with no headline, image or comment box. Code used in this lesson is shown below the video.

Code used in this lesson

<div class="sfcode">
<pre class="brush-php syntax">$loopty_loop = new post_format_loops;

class post_format_loops extends thesis_custom_loop {
function single() {
if ( has_post_format( 'aside' )) {
while (have_posts()) { #wp
the_post(); #wp
$classes = 'post_box top';
echo "ttt<div class="" . join(' ', get_post_class($classes)) . "" id="post-" . get_the_ID() . "">

echo "tttt<div class="format_text entry-content">
echo "<h2>This post has the post format of 'aside'</h2>";
thesis_post_content($post_count, $post_image);
echo "tttt</div>

echo "ttt</div>

// comments_template(); #wp
} elseif (has_post_format('quote')) {
// stuff to display the gallery format post here
} elseif (has_post_format('link')) {
// stuff to display the link format post here
}else {

Video Transcript

Rick: So we’ve added theme support and now we need to start using a couple of standard Thesis functions. The first one is the Thesis… well, now it depends on what you want to do, in the first place. You could… if all you want to do was change some colors then you could easily you know, just add a custom body class to those post formats and then have a different custom CSS for it. And we are going to talk about adding a custom body classes to these post formats but we’re going to talk about that separately. That’s the easy way to do it but… I mean, that’s the easiest way to do but what we really want to do is, I think, talk about the more interesting things like for example, in an aside, you might not want to share the title. In an aside, it might just want to be a little bit of text. And so that means you have to redo the loop, the single loop that Thesis provides. And so that’s what we’re going to do next.

And the way that works… I’m just going to pretend nobody knows anything about the custom loop API and we’ll do a little introduction on the custom loop API. And then we’ll look how to do this with the post formats. And in terms of the custom loop API, the custom loop API is a custom loop class and WordPress… I’m sorry, Thesis has this custom loop class that you can extend to make it do what you want it to do and there’s plenty of documentation on this. Let’s see… if we come over to the Thesis Users Guide and look at this Thesis custom loop API you know, here’s all the documentation about how to use this. The loop that we’re going to work with specifically is the single loop and that applies only to posts. It doesn’t apply to pages or anything else.

And the way you start it off is by essentially creating a new variable for this class. Often, I think, Girlie thought this was cute so she wrote this loopity loop = new my loops. That’s the way it reads in the codex. I prefer to have this be descriptive about what we’re actually doing. And so I’m doing this new post format loops and this is actually a class that we are now creating and the class of post format loops. And this class is going to extend the Thesis custom loop class. Now that is obligatory in the programming for php and it’s kind of a… it’s a technical conversation that we probably don’t need to have. But nevertheless, what you’ve done is you’ve created a new class and you have extended the existing Thesis custom loop class with this new class.

And that extension is dealing with the single loop. So inside of this class, you have function single which means that this is only going to apply to single posts. It’s not going to apply to pages or archives or 404s or tags or anything else. It’s only going to apply to single posts.

And then inside of that single post, you ask questions about you know, which post format it has. And that’s how you decide what content you want to display because if… by definition, Thesis is going to display everything in a single post that you’ve told it to display in Thesis Design Options. And this is your opportunity to change what you want it to display using this custom loop API. And so you ask the question, okay first, if it’s a single post or in the case of single posts, if it has a post format aside then we’ll make some changes. Else, if it has a the post format quote, we’ll make some changes. Else, if it has a post format link, we’ll make some changes. And then otherwise, we’re just going to use the regular Thesis loop single. And so that’s how this works. We’re going to propose changes to the display of single posts when it is either an aside, a quote, or a link.

And in order for us to do that, we need to understand what Thesis includes then in its loop. And so we’re going to actually open up Thesis source code here and since I’m working on this site on Thesis 1.8, that’s the code I’m going to look at. And it’s not in custom though, it’s going to be in Library. And under classes, we can look at loop php and then we can take a look at the single loop here, if is single. And the single loop then calls… while have post, the post, and then classes and the Thesis post box and then the comments template. So we’re just going to go ahead and copy this first. This is under loop php under classes under Library under thesis_18. So we’re going to go ahead and copy that and come over to post formats and paste it. So… and actually, we probably don’t need while have posts. Well, we’ll leave while have posts in this case but we probably don’t really need while have posts. But the post, classes, post box top and then Thesis post box with classes, now this is the part we’re really going to customize which is the Thesis post box. And the Thesis post box can be found under HTML, I believe. No, it’s under functions. You know, I think it’s even content. Thesis post box, there it is.

And so what we can do instead of having Thesis post box then is we don’t want the post image anymore so we’re not going to bother with that. And we don’t need hooks in this case but we may as well keep… yeah, we’re just going to take everything in between those 2 hooks and we’re going to paste that in the place of Thesis post box classes. And okay, we’ve got our classes. That’s going to come in here. Get the idea is fine. We don’t need these hooks and we don’t want the headline area so we can go ahead and cut both of those out. We’re going to go ahead and leave post content in place but we don’t need the post box bottom. Pardon me, we’ll just double check that. This is the hook, right. We don’t need the hook on the side. And for the moment, we’ll leave the comments template in place.

So what we’ve got now is we have created essentially a post that only shows the post content and doesn’t show… and shows the template but doesn’t show the heading, essentially. And if we take this, copy it, move it over to our custom functions php file because I’m not actually working there… let’s see, cut all these custom code in this thing… we might as well come all the way down to the bottom.

Okay so we’ve got our loop. We’ve got our if is single, if format post aside. And actually you know, what I’m going to do here is I’m going to add one little thing to this which is another echo statement. Let’s see, just for grins, echo this post has the post format of aside. Okay so we’re going to keep all the typical Thesis structure but we’re not going to have a title. We’re going to make this little message and then we’re going to show the post content and that’s what’s going to happen in our aside. So we save our document. We upload this document. We refresh this page… wrong page. We refresh this page and now you can see the image went away. The headline went away. Here’s our little message, “This post has the post format of aside.” And now, we have just the content. It also has our leave a comment. And so if we wanted to get rid of that, we would just get rid of the comments template. And if we wanted to… let’s say, h2. So we’ve wrapped this in an h tag, save that. Let’s go ahead and just… we’re going to comment out the comments template for a second, save it, upload it, refresh it. I need to… I must have failed to place the closing… yeah, it wasn’t a closing h2 tag. It was two opening h2 tags. Upload it, refresh it. This post has a post format of aside and then there’s the text and that closing thing is now gone.

So okay, I’m going to unmute your microphone again here, Rachel. There you go.

Rachel: Yup.

Rick: So now, I mean you can do anything you want now that you do this, right?

Rachel: Yeah, yeah.

0 Comments… add one
0 comments… add one

Leave a Comment