Build Your Own Business Website header image

Community Library Website – Part 53 – Show the Term Label Only when a Term Exists

Difficulty Level -

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.

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.

Community Library Website – Case Study Part 1 – Site Organization
Community Library Website – Case Study Part 2 – Create Custom Post Types
Community Library Website – Case Study Part 3 – Custom Post Type Configuration
Community Library Website – Case Study Part 4 – Custom Taxonomy Decisions – Theory
Community Library Website – Case Study Part 5 – Custom Taxonomy Decisions – Practical Application
Community Library Website – Case Study Part 6 – Incorporating Existing Data Into the Project
Community Library Website – Case Study Part 7 – Creating Hierarchical Custom Taxonomies
Community Library Website – Case Study Part 8 – Creating Non-Hierarchical Custom Taxonomies
Community Library Website – Case Study Part 9 – Creating Custom Post Meta
Community Library Website Case Study – Part 10a – Restore the Database Using BackupBuddy
Community Library Website Case Study- Part 10 – Creating Custom Post Meta – Continued
Community Library Website – Part 11 – Importing Existing Data to the Website
Community Library Website – Part 12 – Prepare Existing Data for import by the CSV Importer plugin
Community Library Website – Part 13 – Import Existing Data Using the CSV Importer Plugin
Community Library Website – Case Study Part 14 – Date in Permalink Configuration
Community Library Website – Part 15 – Importing Images
Community Library Website – Part 16b – Final Data Import
Community Library Website – Part 17 – Add Thesis Post Meta to Custom Post Type
Community Library Website – Part 18 – Add WordPress 3.0 Menus
Community Library Website – Part 19 – Add Post Meta and Taxonomy Information to the Post Display
Community Library – Part 20 – Add topics and audience to book display
Community Library – Part 21 – Display the Rest of the Post Meta in the Post
Community Library Website – Part 22, Create the Permalink Structure
Community Library – Part 23 – Remove Custom Post Meta from Post Display
Community Library Website – Part 24, Add Taxonomy and Post Meta Data to the Teaser
Community Library Website – Part 25, Add Taxonomy Data to the Teaser
Community Library Website – Part 26, Adjust the Teaser Styling Using Design Options
Community Library Website – Part 27, Styling the Teaser Using CSS
Community Library Website – Part 28 – Fine Tune the Teaser Display
Community Library Website – Part 29, Designing a Custom Catalog Page
Community Library Website – Part 30, Using a Query String in a URL
Community Library Website – Part 31, Adding Custom WordPress Menus
Community Library Website – Part 32, Add Custom Menus to the Catalog Page
Community Library Website – Part 33, Add Another Custom Menu to the Catalog Page
Community Library Website – Part 34, Troubleshooting Custom Menus
Community Library Website – Part 35, Add Styling to WordPress Menus
Community Library Website – Part 36, Add HTML Markup to WordPress Menus
Community Library Website – Part 37, Add WordPress Menus to Archive Pages
Community Library Website – Part 38, Add and Style a List of Tags
Community Library Website – Part 39, Add a Search Box to the Sidebar
Community Library Website – Part 40, Add Links to the Sidebar
Community Library Website – Part 41, Add Taxonomy Drill Down Browsing
Community Library Website – Part 42, Custom Loop to Display Custom Post Types
Community Library Website – Part 43, Simple Custom Query
Community Library Website – Part 44, Custom Taxonomy Query
Community Library Website – Part 45, Add Thesis HTML Structure to Teasers
Community Library Website – Part 46, Display Posts from the Last 60 Days
Community Library Website – Part 47, Custom Loop If Have Posts
Community Library Case Study – Part 48, Increase the Number of Posts that Display on the Custom Catalog
Community Library Website – Part 49 – Naming Conflicts in Custom Taxonomies
Community Library Website – Part 50 – Find and Replace Semi-colons in the Database
Community Library Website – Part 51 – Modify Taxonomy Names Directly in the Database
Community Library Website – Part 52 – Sort Posts by Meta Value
Community Library Website – Part 53 – Show the Term Label Only when a Term Exists
Community Library Website – Part 54 – Sort Archive Posts by Meta Value
Community Library Website – Part 55 – Add Sort Order to Custom Taxonomy

Video Transcript

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.

0 Comments… add one

Save $200 on Membership Now!

Start learning today for as little as
$0.82 PER DAY!
THIS IS A LIMITED TIME OFFER!
Subscription Options
0 comments… add one

Leave a Comment