Build Your Own Business Website header image

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.

Part 2 – Example #1 – Add a List of Posts from a Single Category

Difficulty Level -

Filed Under Topics - ,

Listed Under Lesson Subjects -

Applies to -

Now that we’ve talked about the different kinds of WordPress Loops and what they do, it’s time to look at the way all the loops work both primary and secondary. We’re going to begin talking about this by looking at the codex under Class Reference/WP Query.

WP Query

The way that works is with this WP_Query class. We’re going to look at an example of it and then once we put the example on the site we’re going to talk about all the aspects of it. I’m going to copy this one brief example here because I think it’s a reasonable example and we’re going to paste it in our byob-custom-functions.php file.

A Note About Our System for Customizing Genesis

This is the functions file that’s created by my plugin, BYOB Genesis Child Theme Editor. If you’re coming tonight without having seen the earlier videos in Customizing Genesis Using PHP, I teach a system that does not use the functions.php file in the child theme.

I think that using the functions.php file in the child theme is poor practice unless you’re the child theme developer, of course. If you’re the child theme developer that’s the place to do it but if you are modifying a child theme then you want to put it in a separate PHP file.

As I said, I have a plugin that will include a separate php file in the right location and give you the chance to make these changes separately. So what you are looking at here is that file.

Create a Function Using WP Query to List Posts by Category

The very first thing we have to do actually is create a function out of it and hook that function in Genesis because that’s the way Genesis works. This is kind of a generic bit of code and so it doesn’t have a function but we’re going to start by creating one.

So function and it’s going to be byob_list_posts_by_category and then we’ll close out that function. Unfortunately, this example that they have isn’t actually presenting you with any query arguments. It’s got a variable here for $args but it doesn’t have a definition for $args so I’m going to provide a definition.

Add Query Argument

I’m going to define this variable $args and if you are lost right now don’t worry about it I’m going to explain all this after it is working.

$args = array and then we’re going to put a query argument in here and the query argument we’re going to put in here is ‘cat’ which stands for category and the category we’re going to display is 2.

Add the Hook

So we’ve got a function, now we have to add that function to some place on this Genesis site and I am going to add the function to the hook genesis_before_sidebar_widget_area for no particular reason.

Anyway, we’re going to add_action as you’re already familiar with from the introductory lesson when we cover using Action Hooks in Genesis. So it’s add_action, the name of the hook that we’re going to add this function to and then the name of the function. We’ll put our semicolon to close out that statement.

So we’ve created a function, the function is this query and the way this works is we are using WP_Query. So we have a variable with our query arguments and the only query argument we care about is the category that we’re going to display.

Define a Query

We are going to display all of the post and category with the id of 2 then we are going to define a query.

The definition of the query, we’re giving it a variable name $the_query and we’re saying it’s equal to a new instance of WP_Query with these arguments. This query then is going to return all of the posts that are in the category id 2 and all of their children.

Once we’ve got that then we act on the query. So we say if the query has posts and then if it does have post then we’re going to display a list of those posts and then we’re going to reset the query. Then we’re adding that whole thing to our site.

That’s a very cursory explanation but I’m going to talk about all of it in detail as soon as we get this loaded on the site and have it working.

Review Changes on the Site

Okay, so here’s the site we’re working on and this is our site, We’re using the Enterprise Pro theme and if we go to say our Blog page, you’re going to see now all of the posts that are in category 2.

Let’s come back over and look at our categories, we have Apples, Green Apples, Red Apples and Yellow Apples. These are children of Apples then Bananas, Cherries, Oranges, Quotes, Single Post and Uncategorized.

If you hover over Apples, you can see down here in the bottom while I’m hovering over it, that the &tag_ID=2. That’s the id of the Apples category. If we hover over Green Apples you can see it’s ID=5, Red Apples is ID=8, Oranges is ID=6 and so on.

Somebody says I spelled category wrong in the function name. I certainly did. Spelling here doesn’t really matter but what does matter is this has to be the same as this because this is calling that.

So these are the categories and we’re saying display all the posts in this category. And in fact, it is displaying all of the posts in that category, Northern Spy, Jonamac, Irish Peach.

Let’s come over and look at our categories and at the Apple posts. Here are all of our Apple posts and those are all showing up here. That’s the result of what we’ve done. So now, let’s take an in depth examination of of this function focusing on the secondary loop.

Save $200 on Membership Now!

Start learning today for as little as
$0.82 PER DAY!
Subscription Options
0 Comments… add one
0 comments… add one

Leave a Comment