Website Building » WooCommerce » How Do I Create a Category Template in WooCommerce?

How Do I Create a Category Template in WooCommerce?

Last updated on January 19, 2023 @ 2:45 pm

WooCommerce is a powerful ecommerce platform that gives you the ability to sell online. One of the great things about WooCommerce is that it’s very customizable. You can add different types of products, change the layout of your store, and even add custom WooCommerce templates to change the look and feel of your site.

One of the most common questions we get asked is how to create a category template in WooCommerce. While WooCommerce does not have a “category template” feature built-in, it’s actually very easy to create one yourself using a few simple steps.

First, you’ll need to create a new file called category-{slug}.php in your theme’s folder. You can name this file anything you want, but we recommend using the slug of the category you’re creating the template for so it’s easy to remember. For example, if you’re creating a template for the “Shoes” category, you would name the file category-shoes.php.

Next, you’ll need to copy and paste the following code into your new category template file:

<?php
/**
* The template for displaying category pages.
*
* @package WooCommerce
*/

get_header(); ?>

<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">

<?php if ( have_posts() ) : ?>

<header class="page-header">
<?php
the_archive_title( '<h1 class="page-title">', '</h1>' );
the_archive_description( '<div class="taxonomy-description">', '</div>' );
?>
</header><!-- .page-header -->

<?php
woocommerce_product_loop_start();

while ( have_posts() ) : the_post();
wc_get_template_part( 'content', 'product' );
endwhile; // End of the loop.

woocommerce_product_loop_end();
?>

<?php
/**
* woocommerce_after_shop_loop hook.
*
* @hooked woocommerce_pagination - 10
*/
do_action( 'woocommerce_after_shop_loop' );
?>

<?php else : ?>
<?php wc_get_template( 'loop/no-products-found.php' ); ?>
<?php endif; ?>

</main><!-- #main -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
PRO TIP: If you are planning to create a category template in WooCommerce, be aware that there is a risk of overwriting existing files and losing data. Proceed with caution and backup your files before proceeding.

This code will create a basic template that pulls in all products from the specified category and displays them using the default product template. You can then customize this template to match the look and feel of your site, or add additional functionality as needed.

Dale Leydon

Dale Leydon

Sysadmin turned Javascript developer. Owner of 20+ apps graveyard, and a couple of successful ones.