We’re at part 3 of Using Secondary WordPress Loops in the Thesis theme. I’ve got this site that I’ve created for the purpose of demonstrating adding a list of post teasers. It has 3 categories of posts, it has Oranges, Bananas and Cherries. It also has Apples and the Apples aren’t in that list. Actually, I should just add that to the list and we also do have the blog page on the list because it’s the front page in this case. Here’s our blog page with all of our various posts.
This is the Thesis theme site that we’re working on and what we’re going to do here is, we have a page and the main loop is just taking this content. If we look at the page, there’s the content, this demonstrates putting a secondary loop of a single category on a static page. This is the page we’re going to work on.
Create a Function to Put a List of Thesis Teasers in a Category
What we’re going to do is create a function that is going to put a list of Thesis teasers in the Apples category and we’re going to show 4 of them here. We’ll kind of come back over to usage because we’re going to start off with their own code. I’m just going to copy this code for a second, open up my file and we’re going to start off by creating a function. This is going to be our simple teaser list, byob_simple_teaser_list and I’m going to paste these things that we got from the website.
We’re going to start off here by defining the arguments that we want to use. We’re going to say, $args = array and the first query parameter we’re going to use is the category. We’re going to use category_name and use this array symbol then the category name we’re going to use is Apples. We could actually just stop there and if we stop right there, what it’s going to do is just display as many Apple posts as I have. In fact, why don’t we just do that, we’re just going to start right there.
We’ve said $args = array(‘category_name’ => ‘apples’ so we just put $args here in our query so we got, $the_query = new WP_Query then we have our loop. While the query has found some posts in the category Apples, retrieve all that information then all we’re going to do is display the title of each of those posts. I’m going to add this to the page, add_action(‘thesis_hook_after_content’), and the name of our function.
We’re going to add this function after content and actually, I want to make it only applies to a single page so I need to add that if(is_page) and I’m going to use the one page carosel so if(is_page(‘carosel’)) then we’re going to get all of the posts that are in the category of Apples and we’re going to display their titles. We save the document, upload it to the site, come back over here and refresh this page, there we go.
Now we have each of the titles of the first 10 posts because in my Settings, Reading, I’ve said blog pages show at most 10 posts. Since this says 10 posts and since I didn’t define anything special in my function, it’s going to display 10 posts.
We don’t really want to display 10 posts, we only want to display 4 which means we need to add parameters to this arguments array. That argument remember, was posts_per_page. If we come back over here and look at that for a second, class reference, pagination parameters, the value is posts_per_page so let’s take that, put a comma there, tab over, posts_per_page and we’re going to say it’s 4.
For readability, what we’re going to do is indent these so that they all line up. Category name is Apples, posts_per_page is 4, we’ve just changed the query argument, save that and upload it. Now, we’re only going to see the first 4 titles displayed.
Obviously, the titles are kind of boring. We don’t really care about the titles, that’s not what we really want. We want more information than just the titles. What I want to use is Thesis teasers so we’re going to take function, thesis_teaser, copy it and instead of this echo thing, we’re just going to paste that. What we’re going to do now is take our Thesis teasers. It takes 3 parameters, the thesis_teaser does $classes, $post_count = false and $right = false.
We actually aren’t going to interact with those, we’re just going to leave them like this. Essentially, it sets all of them to blank which is fine although the thing will crash if we don’t include all those so we’re going to show all 3 parameters that the function, thesis_teaser takes.
Configure Thesis Theme Teasers
What this is going to do now is display the 4 most recent teasers of Apples posts. If we save that document and the way the teasers are going to work is if the teaser is the way we’ve configured our teasers. I’ve got my teasers configured like this so it’s going to have the image, the title, the date, the text and Read the full article, that’s what it’s going to look like. We’ll upload that function and come back to our single loop page. Now you can see we have 4 teasers set up here.
I didn’t include some of the wrapping HTML that would be necessary in order to make this look exactly like it looks over here just because it’s not really part of what I’m trying to teach. The concept here is that you can go grab the Thesis teasers and we could wrap them in HTML. If we get time maybe we’ll do that so that they display this way.
What I wanted to show you is that you can create a loop that will grab those teasers and it’ll take only 4. We can come back in here and change that to 6 and now it’ll display 6 instead. We could change this and instead of being Apples, it could be Bananas and it would change what was being called. That’s really the simple basic loop.