FREELessons: 12Length: 1.3 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

4.1 Configuring the Query Parameters

Hello, and welcome back to this Tutsplus course on WordPress plugin development. In this part of the course, we're going to start working on our third plugin, which is a plugin to display a post from each of three categories at the bottom of each post of each single post screen. So let me just take you to a single post is an example. So at the bottom of the post screen, we will have a list of three posts, one from each of three random categories. And in each case where we'll have the post title, we'll have the exert and we'll have the featured image. So let's get started. Here's my plugin. And as you can see, I've created a folder called tutsplus-category-post-list, and then a file called tutsplus-category-posts-list.php inside that folder, so you can see it here in my file structure. Now it's really important this one that you put it within a folder because we're gonna be adding a style sheet later on inside that same folder. And we'll include the style sheet and add the styling after we've written the PHP. The first thing we need to do is fetch some categories, and we'll do that using the get categories function provided by WordPress. So I'm gonna set up a function to do that. So this is our function tutsplus_category_post_listing. And this function will contain all of the code needed to fetch the categories, set attributes and arguments and then output the content. So the first thing we need to do is define an array of variables that will be used as the argument for the get categories function. So, my first argument is order by this is going to be random. The second argument is the number of categories. Which will be three. So that's my arguments. And if you want to know what arguments you can use for this function, You can take a look at the codex page. So, here's the code reference for get categories, and it tells you what the arguments are. In fact, you have to follow a link to get terms for additional options and arguments. So, we now have our arguments, and then, we need to define an array of variables called categories, and we'll use that in a full reach loop. So categories will be what's returned by the get categories function with those arguments. And now we need to check if that has been populated. So if no categories have been returned, and get categories by default will only return categories that have got posts within them. So if there aren't any categories with posts within them, categories will be empty and therefore it will skip past this conditional statement and nothing will be output, but if that is the case it will run the code within this if statement. So I'm gonna open a section, And you see from the syntax highlighting that I need to close and reopen the PHP back cuz I'm now in HTML. So I've opened this section. I'm now gonna close it as well so I know that's done. And then within that section, I'm gonna have a heading to start with. And because I'm internationalizing this, I'm using PHP and I'm echoing it out. [SOUND] So there, I'm echoing out the h2, and latest posts translated using the tutsplus text domain, which is up here in my plugin header. And while I'm up here in the plugin header, I'm going to change the version number so that it's equivalent to the part of the course that we're on. Let me close the PHP tab, so that this highlighting here isn't incorrect. In fact I won't, cuz this is gonna be a whole lot of PHP, so I'll close them down here. So after adding that heading, I then need to add a for each loop. And I'm running for each categories. As category. So I'll have a single variable called category instead of an array of variables. Called categories, if that makes sense. And then we've got more braces. So within my for each loop is where I'll be outputting the query, because this will run through each of my three categories defined using this number three. And it will run a query to output the content. And I just realized we're in part 4.1. Just go back up there and saying that right back to our for each loop. So what I need to do now is to find the arguments for my query, which I'm using WP query for So, the post_type is post. And to be honest, I don't need to include that cuz the default post_type is post. That helps me just be clear on what exactly I'm doing here. Posts per page, is one cuz I just want one post from each category, and it will be the most recent post from each category because I'm not changing the order by for WP query. The order by for the categories is random so it's going to select 3 random categories but the order by for the posts is the default which is most recent first. The category name. And that's the name of this category using that category variable, which is fetched by the get categories function up here. So those are my arguments for my query. So I've now fetched three random categories. And obsessive arguments using the category, for each category, so that I can run a query. And that's what we'll do in the next part of the course. See you next time and thanks for watching.

Back to the top