This member wants to display only a single category of posts on her homepage. She doesn’t want any of the other posts to be displayed there. She is using a posts page for her home page rather than a static front page. I demonstrate how to add custom query variables to the Homepage Loop.
Rick: Okay so let’s see, moving up next. Pam has a question. She’s hoping I’m going to talk about how to show posts from just one category on the homepage. You know what? I am going to be showing something very, very similar to that and when we do the conversation about the case study. Because what I’m going to be doing is we’re going to be doing categorized posts and so you’ll see the code and I’ll make a point of explaining the code for how to make that work. Now actually, I’m going to turn your volume on here for second here, Pam. I want to make sure I understand what you’re asking. So Pam, your volume is on.
Pam: Yes, good evening Rick.
Rick: Good evening, Pam.
Pam: Yes, can you hear me?
Rick: I can hear you great.
Pam: Yes, I think you understand what I’m trying to do when I saw a picture of a page from the codecs, I should’ve started it. That’s what made me think you were going to cover something like that. On my homepage, I don’t want to just show the latest post. I want it to be sorted just once that were pegged a certain category.
Rick: But your homepage is a blog page, right?
Pam: Yes but because it’s a library, I also have used the blog function to write reviews of books but I don’t want to see those reviews on my homepage.
Rick: So you want to exclude specific categories on to the homepage or do you want to just specify the categories? You want to exclude the categories.
Pam: I can do either one. Yes, what I really want to do is exclude anything that I’ve marked as a review because my dynamic content gallery is showing a rotating series from my reviews. I don’t want to duplicate those on my homepage.
Rick: Sure. Okay so I will show you how to do that separately and we’ll just do that right now.
Pam: That’s great. Thanks.
Rick: Yeah, you’re welcome. So let’s see, and I’m going to show you that…. is this my…which one is that? Oh, okay. This is my personal custom functions php file, is that right? Yes it is. Okay and let’s see, let’s go up to byobwebsite and then down to templates. I think that’s where I specify what goes on a homepage. I had bookmark, homepage customization. Yes, this is where I do it and so this is the method that I use for doing exactly that same thing. There are specific categories of posts that I don’t want to show up in my blog and there are specific tags that I don’t want to show up in my blog. And so what I’ve done is created this custom query and hooked it to the hook before content. Now, you don’t actually need to do it this way. The new system of the custom loop API actually would let you do this a little bit differently but you know, this works in a fairly straightforward way.
Essentially, what you’re doing is you’re modifying the custom… you’re modifying the query and that query is going to essentially say that the you’re not going to use anything in these categories and you’re not… I mean, I’m sorry. You’re not going to display anything in these categories and you’re not going to display anything in these tags. Now one thing I want to point out is that these 2 particular pieces of code actually used 2 underscores. You know, that wasn’t evident to me the first time and I’ve just banged my head against the wall trying to figure out why category not in and tag not in didn’t work. And they didn’t work because right here, you have 2 underscores not a single underscore. So and that just doesn’t jump out at you.
Pam: That’s amazing.
Rick: But so I’m going to take this code and I’ll just walk through it briefly. So you know, first you have a function that well, actually do we really need the function that way? You know, you don’t. In fact, this would probably work…yeah, see what I’d do is I do a bunch of different things or once upon a time, I did a bunch of different things if it was the homepage. So I asked if it’s the homepage up here and if it’s the homepage, then I do the homepage customization. And if it isn’t then I do this customization and so on and so forth.
And so, I’ve got it structured a little bit differently than you might otherwise. What you would do here probably typically would be to say, if is home and then opening brackets and you come past the query posts here, you put closing brackets and then indent that. And so now what happens is… and then what we do is we take this add action and we take it out of that little function and we just have this Thesis at homepage custom query and now homepage custom query runs if it’s the homepage. So your text really or your code really looks like this. If it’s the homepage, and this is not your front page but it’s your homepage, so this all of this refers to the blog page. If it’s your homepage then you create this custom query and your homepage generally has this query post query without any modifiers. So what you’ve done here is you’ve added modifiers to the query post query and those modifiers are category not in and tag not in. And then these numbers are the id numbers of the category so do you know how to find the id number… category id number?
Pam: Yes, I do.
Rick: Okay. So then it’s pretty simple and I will put that in the Live Answers.
Pam: The only thing on my page that’s different is that it wouldn’t work before content because that would put it before my multimedia box that I’m using for the…
Rick: Well, all you want to do is put…no, it’s fine. All you want to do is put the query in there before the query is run.
Pam: Oh I see what you mean. I’ve got it.
Rick: It’s just a query. It’s actually not going to change where it’s displayed.
Pam: Okay, perfect.
Rick: This piece of code…you don’t see the result of this piece of code, right? This is just behind the scenes code and we’re just making sure that it gets asked before the query inside of content gets asked.
Pam: Sure. That’s great. Very simple, thanks.
Rick: So certain post categories and tag from the homepage. Okay, paste that codeand make that php code, post that topic. So there you go and so it looks like that’s pretty much the end of the questions then.