Advertisement
  1. Code
  2. WordPress
  3. Plugin Development

Create a Blog for Each Category or Department in Your WooCommerce Store

Scroll to top
Final product imageFinal product imageFinal product image
What You'll Be Creating

If you're running a WooCommerce store that also has a blog, then you may decide to keep your blog completely separate from your store, with its own categories and content. But for greater integration with your store, it might be a good idea to create blog posts that relate to your product categories.

To do this, you'll need to add the product category taxonomy to your regular posts, create some posts with those product categories, and then display the blog posts on the product category archive pages in your store.

The good news is that WooCommerce provides you with action and filter hooks you can use to do this.

So let's get started!

What You'll Need

To follow along, you'll need:

  • a development installation of WordPress
  • a code editor
  • WooCommerce installed and activated
  • products added to your store (I've imported the dummy product data that comes with WooCommerce—for details of how to do this, see this guide)
  • a WooCommerce-compatible theme activated (I'm using Storefront)

Here's my starting store:

WooCommerce store - front pageWooCommerce store - front pageWooCommerce store - front page

Creating Our Plugin

I'm going to create a plugin to do this, which is better practice than adding it to your theme. So start by creating a file in your wp-content/plugins directory. I've called mine tutsplus-woocommerce-category-blog.php.

Open the new file and add the opening lines:

1
<?php
2
/**
3
 * Plugin Name: Tuts+ Add blog posts to WooCommerce product categories
4
 * Plugin URI: https://tutsplus.com
5
 * Description: Add a list of relevant blog posts to product category archives in WooCommerce
6
 * Version: 1.0
7
 * Author: Rachel McCollin
8
 * Author URI: http://rachelmccollin.co.uk
9
 * Textdomain: tutsplus
10
 *
11
/

Edit the above code to reflect the fact that this is your plugin, and save your plugin file. Now activate the plugin on your site.

Checking WooCommerce Is Activated

I don't want any of the code in this plugin to run if WooCommerce isn't activated on this site. The best way to check WooCommerce is activated is to check if the woocommerce filter exists. If so, I'll add all of the add_action() calls here, and this will fire all of the functions in my plugin.

Add this to your plugin:

1
function tutsplus_check_woocommerce_active() {
2
     
3
    // check if woocommerce is activated
4
    if ( class_exists( 'woocommerce' ) ) {
5
 
6
        add_action( 'init', 'tutsplus_register_productcats_posts' );
7
        add_filter( 'woocommerce_taxonomy_args_product_cat', 'tutsplus_change_productcat_label' );
8
        add_action( 'woocommerce_after_main_content', 'tutsplus_display_productcats_posts' );
9
 
10
    }
11
	
12
    else {
13
        return;
14
	}
15
16
}
17
add_action( 'plugins_loaded', 'tutsplus_check_woocommerce_active' );	 

Adding Product Categories to Posts

The next step is to register the product_cat taxonomy for the post post type. You do this using the register taxonomy for object type() function.

Add this to your plugin:

1
function tutsplus_register_productcats_posts() {
2
     
3
	 register_taxonomy_for_object_type( 'product_cat', 'post' );
4
	 
5
}

This adds the product_cat taxonomy, which WooCommerce uses for product categories, to blog posts. Note that I haven't hooked it to an action, as that's already done in my check for WooCommerce.

Now, if you take a look at your admin screen, you'll see that you have two taxonomies for posts called Categories:

Two category taxonomies for postsTwo category taxonomies for postsTwo category taxonomies for posts

That's a bit confusing, so let's change the label used in the admin menu for product categories. WooCommerce gives us a filter we can use to do that, called woocommerce_taxonomy_args_product_cat.

So add the function below to your plugin:

1
function tutsplus_change_productcat_label( $args ) {
2
    
3
	$args['labels'] = array(
4
        'label'         => _x( 'Product Categories', 'tutsplus' ),
5
        'menu_name'     => _x( 'Product Categories', 'Admin menu name', 'tutsplus' ),
6
        'add_new_item'  => _x( 'Add new product category', 'tutsplus' ),
7
	);
8
9
	return $args;
10
11
}

This will change the name of the product_cat taxonomy to Product Categories in the menu.

Refresh your admin screen and check it:

Product categories correctly labelled in the admin menuProduct categories correctly labelled in the admin menuProduct categories correctly labelled in the admin menu

That should reduce some confusion (although you'll find it doesn't remove it completely as it's not possible to change the label in Quick Edit, which we'll be using shortly).

Note: You might decide to remove the standard category taxonomy instead. In that case, use the register_taxonomy() function to register it for an empty array of post types, which effectively removes it.

Now, when you add a post, you can assign product categories to it. The way you do this will depend on whether your site is using the Gutenberg editor or not, as WooCommerce product categories don't work with Gutenberg yet.

If you have Gutenberg enabled, you'll have to add the product categories using the Quick Edit link beneath the post name on the main Posts screen:

Adding a product category in quick editAdding a product category in quick editAdding a product category in quick edit

Make sure you don't confuse the two taxonomies called Category here—there isn't a way to rename product categories in Quick Edit.

If you're using the classic editor, you can add product categories to your posts using the post editing screen:

Adding product categories to a  postclassic editorAdding product categories to a  postclassic editorAdding product categories to a  postclassic editor

It's likely that over time, WooCommerce will become compatible with Gutenberg, and this situation will change.

Now create a few posts and give them different product categories.

Outputting Posts in the Product Category Archive

The second step is to output those posts in the product category archive. You do this by creating a loop using WP_Query and then using the woocommerce_after_shop_loop action hook, which WooCommerce runs after outputting products on the archive pages.

If you want to check out this hook, take a look at the templates/archive-product.php file in WooCommerce. If you prefer, you might want to use a different hook to output your blog posts in a different place.

Setting Up the Function

Create a new function in your plugin file, making sure it has the name you used when hooking it to the woocommerce_after_shop_loop hook earlier on:

1
function tutsplus_display_productcats_posts() {
2
    
3
}

Now let's populate that function.

Checking We're on a Product Category Archive

First, add a conditional tag to check if we're on a product category archive, as you don't want blog posts displayed on the main shop page or the search page. Inside your function, add this:

1
if ( is_product_category() ) {
2
3
}

Now your code will only run on the product category archive pages.

Identifying the Current Product Category

Next, inside the braces of your conditional check, add a line to get the current queried object, i.e. the current product category:

1
$productcat = get_queried_object();

Now we can use that variable when setting up our query arguments. Next, add this:

1
$args = array(
2
	'post_type' => 'post',
3
	'product_cat' => $productcat->slug,
4
	'posts_per_page' => 5
5
);
6
$query = new WP_query ( $args );
7
if ( $query->have_posts() ) { ?>
8
        
9
<?php }

This defines our query arguments, runs the query, and checks if it has any posts. Next, we need to add a loop inside that if statement:

1
<section class="productcat-posts">
2
    <?php echo '<h2>' . esc_html__( 'Blog Posts', 'tutsplus' ) . '</h2>'; ?>
3
4
		<?php while ( $query->have_posts() ) : $query->the_post(); ?>
5
6
		<?php //contents of loop ?>

7
		<article id="post-<?php the_ID(); ?>"<?php post_class(); ?>>
8
		
9
			<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
10
			<?php the_excerpt(); ?>
11
            <a href="<?php the_permalink(); ?>"> Read More...</a>
12
		
13
		</article>
14
15
		<?php endwhile; ?>
16
17
	<?php wp_reset_postdata(); ?>
18
	
19
</section>

This opens a new section tag and outputs the blog posts inside an article tag. Make sure you use wp_reset_postdata() at the end to reset the query.

This is what your full function will look like:

1
function tutsplus_display_posts_in_category_archives() {
2
    
3
	if ( is_product_category() ) {
4
		
5
		$productcat = get_queried_object();
6
		
7
		$args = array(
8
			'post_type' => 'post',
9
			'product_cat' => $productcat->slug,
10
			'posts_per_page' => 5
11
		);
12
		$query = new WP_query ( $args );
13
		if ( $query->have_posts() ) { ?>
14
		
15
			<section class="productcat-posts">
16
				<h2>Blog Posts</h2>
17
			
18
					<?php while ( $query->have_posts() ) : $query->the_post(); ?>
19
			
20
					<?php //contents of loop ?>

21
					<article id="post-<?php the_ID(); ?>"<?php post_class(); ?>>
22
					
23
						<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
24
						<?php the_excerpt(); ?>
25
                        <a href="<?php the_permalink(); ?>"> Read More...</a>
26
					
27
					</article>
28
			
29
					<?php endwhile; ?>
30
			
31
				<?php rewind_posts(); ?>
32
				
33
			</section>
34
		  
35
		<?php }
36
		
37
	}
38
}
39
add_action( 'woocommerce_after_shop_loop', 'tutsplus_display_posts_in_category_archives' );

Now save your plugin file and check the archive page for a product category you've added posts to. In my Clothing category archive, if I scroll down past the products, my posts are displayed:

Blog posts displayed on a category archive pageBlog posts displayed on a category archive pageBlog posts displayed on a category archive page

Now you have your latest blog posts for the product category displayed on its archive page.

Summary

Having a blog that's integrated with the departments of your store can help you sell more products and make your blog more relevant to customers. In this tutorial, you've learned how to add blog posts to category archive pages, providing your customers with extra content.

Don't forget that the open-source nature of WordPress also makes it a great option for improving your programming skills. Follow along with some of our other WooCommerce tutorials here on Envato Tuts+, or check out some of the excellent eCommerce plugins available for sale on CodeCanyon.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.