This is the 53rd in a series of live lessons on using WordPress and Thesis as a Content Management System (CMS). In this lesson we revise the code we wrote previously so that when there is no taxonomy term associated with a post we don’t display the taxonomy label. We use the get_the_term_list() function to accomplish this.
Now we are going to roll ourselves into the Community Library Case Study and there has been one problem that those of you who have been here the last couple of weeks have seen me flail around at and fail 2 weeks in a row. Well, this time, I succeeded. And in fact, you can see the seeds of that success on a page itself. So let’s look at that first.
If we come over here to our custom catalog, here’s what we’re trying to do. We are trying to make it so that when there is not a topic listed for a book, that topic doesn’t show up. So here’s an example of where it doesn’t look right and these fiction here’s topics and it’s blank. And then if you go to the page itself, you can see I actually succeeded in removing it here without the rest of the site being bulldozed as it was last week and the week before. If you look at non-fiction, pick a book, you see listed under topics and then there’s our topics list. So that’s what it looks like when there’s a topic and there’s nothing there when there isn’t a topic. So that’s the desired result. Now, I’m going to show you how I got there and then we’ll fix the teasers together.
If we go over to custom functions php and we’ll go down and look at the one I fixed which is this one right here. So we have our byob topics author terms and this function adds the topic tags and audience taxonomy to the single post. So if it’s single and if it’s a catalog item, if the post type is a catalog item then add this stuff. Now, the way I had been doing it is using this phrase “the terms” post id and then the name of the taxonomy. And that put out a terms list that was clickable. Well, the way it turns out is that there’s another very simple to use piece of code that works very nicely if you want it to display nothing when there is nothing. And see, here in this case, we’re going to automatically display audience and if there was no audience term, it’ll still say audience and then it would still have these you know, opening P tags and closing div tags and all the rest of that stuff. And that was what we had before when we had listed under topics up here. That got printed no matter what. And in this case though, the way get the term list works is you identify the post id and then you identify the taxonomy and then you specify what you want to go before and then you specify what you want to go in between each of them and then what was this one? Let’s see… actually, I forgot what that one is. Let’s just take a look at the… let’s take a look at the codex for that, get the term list. Oh, this is after, right. So the first part is the id, the 2nd part is the taxonomy, the 3rd part is the before, the 4th part is the separators between each term and the 5th part is after. And so, in our situation, what we have is list in our topic… well, post id, the taxonomy is topics, listed under topics, colon space is what we put before and then what we put in between is a space. And then we aren’t putting anything at all in the case where there is… well, we aren’t putting anything on the end. That’s what that is.
And so, if there are no terms under topics, it returns nothing. It doesn’t print this stuff. It returns absolutely nothing which was what we wanted. And so, it actually totally makes this job a lot easier than I was making it before. So we’re going to do that again.
I’m just going to copy this because this is exactly the same code and we’re going to scroll down here to where we put it in our…in here. So we’re going to come right down here and we’re going to replace this with that code and then we’ve got our opening p tag. Now, we’re just going to get rid of that topics space thing and delete it. And although I do need a closing single quote… and you know, I could actually, well let’s see. In fact, let’s do that just for fun. We’re going to do this slightly differently. We are going to say in this case, we’re going to use p or yeah… we’re going to use a closing p tag and then class byob teaser topics. Actually no, we need that closing p tag. I was going to put all that stuff in here but we’re not going to put all that stuff in here. We are going to put this in there though so we’ll cut this out…control X and we’ll put that in so this starts the thing off. And then we’ll put this in as the after so starts off with some HTML and then topics and the space separators and then closing p tag. And that will give us the whole structure that we want and none of that stuff will be printed if there is in fact no topics. Let’s go ahead and save this and then we’re going to upload both of these things that is, both our custom css and our custom functions php file. Upload that to the site, come back over to the site, refresh this… did we fail to refresh? Okay, so here’s the case where it works where we want it to. We go to our custom catalog. We go to… let’s just scroll down here to a place where there may not be any topics. While you can see it says topics adoption biography grief you know, we could put commas in here if we wanted to. And then when there are no topics, nothing shows up at all. So we have a success here.
So if we look at fiction for example, you can see that none of these things have the topics there. And if we go to one of those books, topics doesn’t show up here either. And so, tada! I mean, I think we’ve only flailed around on that thing like probably what, an hour and 20 minutes… an hour and 10 minutes? Something like that? Finally got it to work. I feel so good about myself now.
Okay so then so that’s how to switch out our… that’s how to get rid of the topics.