Create A Product Listing With Infinite Categories Using Custom Post Types

In today's tutorial we're going to leverage the power of custom post types, taxonomies, and custom loops to create a dynamic products listing. There’s a lot of cover so let's get straight to it!

Step 1 Creating a Products Custom Post Type

First, we need to create our products custom post type. Open your functions.php and add the following line to the top of the file after the opening PHP tag:

If you don't have one already, create a directory called functions inside your theme folder, and a file called products.php inside that directory. In our products.php, we'll register our custom post type:

The first line is a hook that will call our function ‘products_register’ when WP admin is initialized. Within our function we set up two arrays. The first is the labels that will be used throughout our WordPress dashboard. Within our function we create a list of arguments most of which are self-explanatory. Our last argument ‘supports’ allows use to specify what we want to display on the post’s write/edit page. Note that we have added support for page attributes to specify the order of our products within a category (image of panel). We then pass the function register_post_type our custom post type’s name and the arguments.

Order Panel

Let’s add a custom field to store the price of our products.

Again when WP admin is initialized, our function price_meta is called that will display our custom field. Next we create a function called price that will create the form field for our custom post type. we've also grabbed it’s current value using get_post_custom so we can display it when we're editing a post if a value has been set. Our last function saves the value assigned to our custom field using update_post_meta(). We pass the function our post’s ID, the name of the field being updated, and the value we're storing.

Step 2 Creating a Products Category Taxonomy

We want to be able to organize our products into categories so let's create a custom taxonomy called Product Categories so we can do just that.

Again we define our labels. Next we call the function register_taxonomy. We tell the function the name of our taxonomy which is product_categories (note the lack of spaces and capital letters), the post type(s) to associate the taxonomy with and pass it a handful of arguments. Importantly, we set hierarchical to true to support nested taxonomies or in our case sub product categories.

Step 3 Dealing With Images

Our products will be making use of featured images so we need to make sure our theme supports it. Check your theme’s functions.php and see if it already supports post thumbnails, if not add this line to the top of the file just under where we required our custom post type.

We would like assign each of our product categories an image. For now there is no built-in way to do this so we'll need to install the fantastic Taxonomy Images plugin by Michael Fields. Once we have the plugin installed and activated we navigate to it’s settings page and tell the plugin to add image support to our Product Categories taxonomy.

Taxonomy Images Plugin Settings Page

Next we're going to add support for dynamic image resizing for our product category and product images using Timthumb, a PHP image resizing script. Timbhumb is a joy to work with and a breeze to implement in WordPress themes. We’ll get into the meat of using it in our theme files soon but for now we're just going to get it set up. Let’s download Timthumb and place the script in with our theme files. We’ll also need to create a cache directory in our theme’s directory and give it 777 permissions.

Step 4 Listing our Top Level Product Categories

To list our top level product categories we're going to create a custom page template and call it Products.

Next we're going to create a query for our top level categories.

Here we've set up three arguments to pass to the get_categories function. Each argument is essential to retrieving the top level product categories

  • Hide_empty => 0 Allows us to show categories that don’t contain any posts
  • Parent => 0 By defining a parent we've limited the taxonomies returned to only taxonomies that have a parent with an ID of 0. By setting the parent ID to 0 it means we'll return top level taxonomies since they don’t have a parent
  • Taxonomy => ‘product_categories’ here we're defining the taxonomy to query, by default this is set to “category” so we need to tell WordPress we want to query our custom taxonomy instead

Next we pass the arguments to get_categories which returns an array of matched taxonomies. We start a foreach loop and set a couple of variables.

The first is the product category's image that we've retrieved using get_option and passing it the name of the option we want to retrieve. In this case it’s the image set by the taxonomy images plugin. The next variable is the URL of the taxonomy's archive. we've used the get_term_link function to achieve this by passing it the taxonomy slug and the taxonomy name. Next we start outputting the matched product categories.

Let’s take a look at how the thumbnails are generated using timthumb. We need to set the source to the path of our copy of the script in our theme’s directory. Then we need to give the script the source for the image and set it’s width and height. Simple isn’t it? I’ve also set up an if statement to make sure we have a fallback in place if a category doesn't have an image associated with it.

Next we should add some basic styling to make things take shape. we've just floated the divs and absolutely positioned the titles within the divs.

You'll notice we've added styles for the products lists as well since we'll be displaying them in a similar fashion to the product categories lists.

Step 5 Listing Subcategories and Products

Create a file called taxonomy-products_categories.php. If you take a look at the template hierarchy you’ll see that this the template we need to create a taxonomy archive. This allows us to create different archives for any custom taxonomy.

The first thing we'll be doing in this file is querying our product categories again.This query serves two purposes. We’ll be using it to output the next level of product categories if one exists and testing whether or not we're on the lowest depth category and if that’s the case we'll need to output that category's products. So let's set that up.

First we've grabbed the current taxonomy's slug using get_query_var(). Next we retrieve all term data with get_term_by() by passing it the field we're supplying, the value of the field and the taxonomy name. With that we can retrieve the current taxonomy's ID using get_term_by which we need for our arguments array for the parent parameter. Finally we've passed our arguments to the get_categories function and defined a variable for the returned array called categories.

Let's quickly look at the structure we'll be following now:

Now we're going down the route of if the array is empty. If it is, then we're on a lowest products category level and should output the current categories products. Let’s set up our arguments first:

Let's look at the arguments we've set up. The first two arguments enable pagination. The next three arguments handle the order of the products. We're ordering them by the meta key price and also ordering by their price, lowest to highest. If you want to order the products by setting their order when creating a product use the following argument instead:

The final array of arguments defines the taxonomy we're querying and what specific product category we're querying. We define the product category by passing ‘term’ the slug of the current product category.

Next we pass these arguments to the WP_query class which will fetch the matched posts. All we need to do then is loop through the returned array of posts and output our products.

We've retrieved the post thumbnail URL, to use with timthumb, by retrieving its ID and then using wp_get_attachment_url and passing it the ID.

As a fallback, if a category doesn’t contain any products yet, let's add the following:

Finally we'll include our pagination links:

Now we're going to go down the other route, if the category did contain sub categories:

We've already covered what happens in the above code in step four so let's move on the last part of the tutorial.

Step 6 Displaying an Individual Product

If you’ve worked with custom post types before you know what’s coming next. We're going to create a single-products.php. Similar to our taxonomy-product_categories.php we used for our product categories taxonomy, we can create a custom single post template unique to our custom post type. In that file we're going to put:

You should be familiar with most of the template tags used here but let's take a look at the use of get_post_meta that retrieves our price custom field we set in step one. We’ve set up an if statement to check if a price has been set, if one has, we output the price. We do this by passing the function the post ID, the meta key we want to retrieve, and whether we want a single result. We do, so we set it to true.

Again let's add some basic styles to layout our products:

Wrapping Up

We’ve covered quite a bit here but hopefully now you should have a learned some new techniques and gained an understanding of the power of custom post types, taxonomies, and custom queries. Any questions or ideas for improvements? Let’s discuss them in the comments.



Related Articles