WooCommerce: Generate Links To Categories and Tag Archives

Learning WooCommerce Development by Example
A Friendly and Accessible Guide to WooCommerce Development
Click here to get the book now for $29

Problem

Online stores will typically need to link to the categories of products sold in the store, but they may not want to hard code the links into the sites HTML as changes to the URL structure could then break links within the site.

One way around this problem is to use PHP code to generate the links for us, in this article, we’ll take a look at how to programmatically generate links to WooCommerce categories and tags.

Solution

The solution is pretty simple, we need to utilize the WordPress  get_term_link function

//generates a link to the hoodies category
get_term_link('hoodies','product_cat')

//a numerical id for the category can also be used
get_term_link(44,'product_cat')

Here we use the get_term_link function, we pass it two arguments

  • The first argument is an id for the category, this can either the categories numerical id or it’s slug
  •  The second argument is the term or taxonomy name, in this case, we pass “product_cat” which is the name given to WooCommerce categories

As product tags and categories are both terms/taxonomies we can generate links for tags using very similar code

//generates a link to the hoodies category
get_term_link('fruity','product_tag')

//a numerical id for the category can also be used
get_term_link(44,'product_tag')

As you can see, the code is almost identical, we just pass a different term or taxonomy name.

Generating Links to Multiple Categories or Tags

Another benefit of this approach is that we can use it o quickly generate multiple links, in the code below we use the get_term_link function in a foreach loop to generate a link for every category in our store

$product_categories = get_terms('product_cat', array());

foreach ($product_categories as $category) {
	echo "<a href='" . get_term_link( $category->term_id,'product_cat' ) . "' target='_blank'>Take me to the " . $category->name . " category</a><br>";
}

Here we use the get_terms function to return a collection of categories, we pass two arguments to get_terms

  • The term or taxonomy name, in this case, we pass “product_cat” as we only want to see product categories
  • An arguments array, as we are happy with the defaults we just pass a blank array

We then loop over the collection of categories using get_term_link and the category name to generate a list of links, the code above will produce the output below

As with our previous link generating example, we can achieve the same result for tags with similar code

$product_tags = get_terms( 'product_tag', array() );

foreach ($product_tags as $tag) {
	echo "<a href='" . get_term_link( $tag->term_id,'product_tag' ) . "' target='_blank'>Take me to the " . $tag->name . " tag</a><br>";
}

Aside from some differences to the output, the only real difference here is that we pass “product_tag” as the term/taxonomy name.

Excluding Empty Categories/Tags

You may have noticed that in the categories example above, we returned the “Uncategorized” category. Obviously, I run a very well organized store, so I don’t have any products in that category. So how could I stop this category from being displayed?

Luckily the arguments array that we pass to the get_terms function can handle this for us, we just need to add a value/key pair to the array that specifies that we don’t want empty categories to be returned.

$product_categories = get_terms( 'product_cat', array('hide_empty' => true) );

Conclusion

The WordPress get_term_link function allows us to easily generate links to category and tag pages, we can also use it in conjunction with the get_terms function to generate lists of links to categories and tags in our store.

If you have any questions or queries about this post then please don’t hesitate to let us know in the comments.

Hey there! Hope you enjoyed that article, thanks for sticking with it until the end 🙂

If you enjoy our WooCooomerce writing you might enjoy our book "Learning WooCommerce Development By Example", it's only $29 and contains a multitude of useful information for WooCommerce developers. Click here for more details.

You might also like to sign up for our newsletter, where you'll be kept to date with all our latest WooCommerce articles. Just complete the form below to get the latest Hard Working Nerd WooCommerce content delivered straight to your inbox.

Leave a Comment