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.
[This post contains video, click to play]
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.