Build Your Own Business Website header image

Community Library Website – Part 42, Custom Loop to Display Custom Post Types

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 42nd in a series of live lessons on using WordPress and Thesis as a Content Management System (CMS). In this lesson we create the custom loop for our custom catalog page. This custom loop will display recent posts organized by custom taxonomy. We use the WordPress function get_terms() to look at the taxonomy term array to understand what parts of the taxonomy we will be referencing.

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

Well in the meantime though, with the data that we do have, we’re just going to go ahead and pursue I think, probably, the most difficult part of the…most intellectually difficult part of the project which is this custom loop that we’re going to create. We’ve got this page that we want to display the catalog items from the last 60 days sorted by their location. And rather than just dumping all the code on you for how to accomplish that, I’m going to take it sort of step by step conceptually and maybe, we’ll get a better understanding of how this, the loop processes work and how custom loop with custom taxonomies and custom post types work.

And so, the way we’re going to accomplish this is that first, we’re going to get a list of the location names and then we’re going to do what’s called the for each loop that says essentially, for these location names, go get and display all of the catalog items that have been added in the last 60 days. And then if that location name doesn’t have any items in the last 60 days then we don’t want to display anything.

And so, the first step of doing this is to get the location names. And so, that involves obviously, creating our function and then we’re going to use a WordPress command called… or WordPress function called get terms. So actually, in order for this not to take too long, I started the function off here. What we have is create a function name, byob recent catalog items and we’ve added that function to the Thesis hook after content so it’s going to show up after any content that we have on that page. And then speaking of that page, we only want this loop to exist on the one page so if it’s the page, browser search the library catalog then we’re going to do the rest of this stuff.

Now the way this… so now, let’s go take a look at this get terms here and if we come back over to Chrome for a second… but not that piece of Chrome. And we just search for get terms, go to the codex, you can see the syntax here is get terms and then the first parameter is taxonomies and then the 2nd parameter is the arguments. Now, the taxonomies can either be a single term or it can be an array of terms and those essentially are the taxonomy names. The taxonomy name that we’re working with is called location. So when we get the terms, the result is going to be an array of the terms of the taxonomy location.

Now there are other arguments that we can add which we will add in a few minutes. But at first, what we’re going to do, I want you to understand what terms exist in a taxonomy so that you know what we’re looking for then we’ll start to refine this. And so if we go back over to our function here, so we’re going to get the terms and the taxonomy that we’re getting is location. So we’re going to get all the terms for the taxonomy location and we’re going to put those terms in a variable called location names. And now, location names is going to end up being an array as well. It’s because there is more than one term here. And so what WordPress had php do when you’ve got more than one element in a variable, it goes to an array. And in our case, it’s an associative array which means that each element of the array has a name and then a value. And that’s what you see most often working here in WordPress is an array has a name and a value pair. And then for each of those elements, we’re going to have this loop that says for each location names… so for each element in the array, location names, as location names. So it’s going to break out one element of that array and put it in a variable called location name. Then we want to print r which is print to the screen the terms of location name. So what this is doing is this is essentially printing the value of the array. And that’s what we see here on the site right now.

If we come back over to the site and go to custom catalog, that code has produced this whole bunch of text. Now, it looks kind of complicated but I want you to see is that the first element of the location taxonomy starts here with term id and ends here with count. So that’s our first element of that array. The second one starts with this term id and ends with this count. The 3rd one starts with the next term id and so on and so forth. So these are the various arrays. And then inside of that element is an associative array which is the first element inside that associative array is term id. That’s the name of the value and here is the value itself, 160. So the term id is 160, the name is audiobooks, the slug is audio-books, the term group is 0, the term taxonomy id is 160 and the taxonomy is location.

Now description has nothing, it’s blank. Parent has nothing or it’s 0. And count which is the number of posts in the database with this term or with this taxonomy term applied to it is 17. And so that would be the first element of the array and so that little piece of text was printed by this function, print r location name. Now the thing is that we went and got all of our location names and put them into this array and it’s cycled through each one of them. So each time it came to a new location name, it prints it again and then it gets finished, comes next location name and it prints it again. And that’s why we have this you know, long list of them because you’d get to the first one, to here and then the 2nd one starts here and then the 3rd one starts here and then the 4th one starts down in here someplace. And it’s just repeating itself over and over and over again. In fact, if we wanted to separate those things out, we could in fact, maybe that would be interesting. We could add an HTML structure, echo and then put a paragraph tag in front and a closing paragraph tag at the end, echo closing p. Get outside of that and put our closing semi-colon. Save it and now each one of those things is going to be in like a paragraph.

So if we open this up and reload it, come back over to the page and refresh it, now each of those is going to be broken up into… there’s going to be a paragraph ending at each one. So now you’ve got the first location and the 2nd location and the 3rd location and the 4th location. That makes it easier for you to see and what each one of these is a cycle of that loop, of that for each loop.

Okay so that’s the for each loop. Now let’s refine that a little bit. So we’ve got associative array with our name value pairs. We’ve added a print r to that so that we could see some of these taxonomy terms like term id, name, slug. Now, you may have never seen print r before. What you’re familiar with is echo. Print r is what you use for an array. If you put echo here instead, it’s not going to print properly. But I want you to see what it looks like because when you see this result, you’ll know what happened. So if we say echo location name instead of print r location name, we save that, we upload this and then we refresh this… oh that’s interesting. I wasn’t really expecting it to say array because I see the object class can’t be represented that way. So it doesn’t give you the same error. This is the error which is object class standard class could not be converted to a string. And this get the terms returns it to us as an object. I thing I could have said in here that I wanted it…yeah, that’s what happens. It comes to us as an object rather than as an array and for your purposes, it doesn’t matter. There’s function and the same thing but… so just grab that. It didn’t show what I was expecting it to show.

So we’ll go back over to this now and instead of doing this for each, location names is location echoing this stuff, what we’re going to do instead of that is we are going to… let’s see, what are we going to do? We’re going to refine the taxonomy and then we’re going to refine the output.

So by refining that taxonomy, we’re going to add an argument here essentially. So in our get terms, we give the taxonomy name. Now what we want to do is we want to tell it what term within that taxonomy we want. And then term that we want here is names. So we’re going to use this, we’re going to use fields equals names as a modification to the query or as an argument here. And the way we do that is… yeah, okay. We’re going to do that the easy way. We are going to come over here and say get terms location and then we’re going to add the other argument which is… start off with a comma and then our single quotations. And then we’re going to say fields equals names.

Now location name is no longer an array. Now location name is the name inside the field name, inside the taxonomy. So if we save this and we upload it and we refresh it… so now we have a list of all of the name value of the taxonomy location. So audiobooks, bible and reference, biography, so on and so forth.

So we started getting something what we’re looking for that is, we’ve ended up with our list.

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