How To Show One Category On Woocommerce Shop Page

How to Display Only One Category on Your WooCommerce Shop Page: A Comprehensive Guide

Introduction

WooCommerce, the leading e-commerce platform for WordPress, offers a flexible and customizable shopping experience. By default, the shop page often displays all your product categories. However, you might want to showcase a specific category for marketing purposes, promotional campaigns, or to streamline the user experience. This article provides a detailed guide on how to show only one category on your WooCommerce shop page, helping you tailor your online store to meet your specific needs. We’ll explore code-based solutions, plugin options, and discuss the pros and cons of each approach.

Main Part

There are several ways to achieve this goal. The best method depends on your comfort level with code, your current theme setup, and the desired level of control. Here are the most common approaches:

1. Using `pre_get_posts` Filter in `functions.php`

This is a code-based approach that involves modifying your theme’s `functions.php` file (or a custom plugin) to alter the default query for the shop page. This is the recommended approach for developers who want full control and understand PHP.

Here’s how to do it:

1. Access your `functions.php` file: Navigate to your WordPress dashboard, go to Appearance > Theme Editor, and locate the `functions.php` file of your active theme. Warning: Always create a child theme before modifying core theme files! This prevents your changes from being overwritten during theme updates.

2. Add the following code snippet:

 add_action( 'pre_get_posts', 'custom_pre_get_posts_query' ); 

function custom_pre_get_posts_query( $query ) {

if ( ! is_admin() && $query->is_main_query() && is_shop() ) {

$query->set( ‘product_cat’, ‘your-category-slug’ );

$query->set( ‘posts_per_page’, -1 ); // Show all products in the category

}

}

3. Replace `’your-category-slug’` with the actual slug of the category you want to display. You can find the category slug by going to Products > Categories in your WordPress dashboard. Hover over the category and look at the URL in the bottom left of your browser; the slug is usually the last part of the URL after `taxonomy=product_cat&tag=`.

4. Save Explore this article on How To Test Woocommerce Payment the `functions.php` file.

5. Check your shop page. It should now display only the products belonging to the specified category.

Explanation of the Code:

* `add_action( ‘pre_get_posts’, ‘custom_pre_get_posts_query’ )`: This hooks into the `pre_get_posts` action, which allows us to modify the query before it’s executed.

* `custom_pre_get_posts_query( $query )`: This is the function that will modify the query.

* `if ( ! is_admin() && $query->is_main_query() && is_shop() )`: This condition ensures that the code only runs on the front end, for the main query, and on the shop page. This prevents conflicts with other parts of your website.

* `$query->set( ‘product_cat’, ‘your-category-slug’ )`: This sets the `product_cat` query variable to the desired category slug, effectively filtering the products displayed.

* `$query->set( ‘posts_per_page’, -1 )`: This Explore this article on Woocommerce How To See Customer Order History sets the number of posts per page to -1, which displays all products in the category without pagination. You can adjust this value if you want to limit the Explore this article on Google My Business How To Integrate Store With Woocommerce number of products shown per page.

2. Using a Shortcode

This approach involves creating a specific shortcode and placing it on the shop page. While not as direct, it offers flexibility in terms of where you display the category’s products.

1. Create a Custom Shortcode: Add the following code to your `functions.php` file:

 function custom_product_category_shortcode( $atts ) { $atts = shortcode_atts( array( 'category' => '', // Category slug 'per_page' => '12', // Number of products per page 'columns' => '4', // Number of columns 'orderby' => 'date', // Order by 'order' => 'DESC', // Order direction ), $atts ); 

$category = sanitize_title( $atts[‘category’] );

if ( empty( $category ) ) {

return ‘Category not specified.’;

}

$args = array(

‘post_type’ => ‘product’,

‘posts_per_page’ => intval( $atts[‘per_page’] ),

‘product_cat’ => $category,

‘orderby’ => $atts[‘orderby’],

‘order’ => $atts[‘order’],

);

$loop = new WP_Query( $args );

if ( $loop->have_posts() ) {

ob_start(); // Start output buffering

echo ‘

    ‘;

    while ( $loop->have_posts() ) {

    $loop->the_post();

    wc_get_template_part( ‘content’, ‘product’ );

    }

    echo ‘

‘;

wp_reset_postdata(); // Reset post data

return ob_get_clean(); // Return the buffered output

} else {

return ‘

No products found in this category.

‘;

}

}

add_shortcode( ‘product_category_only’, ‘custom_product_category_shortcode’ );

2. Use the Shortcode on Your Shop Page: Edit your shop page in WordPress and insert the shortcode:

[product_category_only category=”your-category-slug” per_page=”12″ columns=”4″]

Replace `”your-category-slug”` with the actual slug of your desired category. You can also customize `per_page` and `columns` as needed.

3. Using WooCommerce Category Page Plugin

There are numerous plugins designed to enhance WooCommerce functionality. Some of these plugins allow you to create and customize category pages, effectively turning them into your primary shop page display. Some example of this plugins:

* WooCommerce Category Pages Designer

* WooCommerce Category Slider

* Category wise Product Listing for WooCommerce

While the plugin installation process is common for all the plugins so, the steps below shows just the basic plugin using process.

1. Installation: In the plugin installation process make sure you install and activate one of the plugins mentioned.

2. Configure: After activating, navigate to the plugin’s settings and look for options to assign a specific category page to be displayed as your main shop page. Configure options such as the number of products to show, the layout, and other design elements based on your theme and its WooCommerce integration.

Comparison of Approaches:

| Feature | `pre_get_posts` Filter | Shortcode | WooCommerce Category Page Plugin |

| —————– | ———————— | ———————– | ——————————– |

| Complexity | Medium | Medium | Low |

| Code Required | Yes | Yes | No |

| Flexibility | High | Medium | Medium |

| Performance | Best | Good | Good/Average (depending on plugin) |

| Maintenance | Requires coding knowledge | Requires coding knowledge | Plugin updates may be necessary |

Conclusion

Choosing the right method to display only one category on your WooCommerce shop page depends on your technical skills and specific requirements. The `pre_get_posts` filter offers the most control and performance but requires coding knowledge. Shortcodes provide a flexible alternative, while plugins offer a user-friendly solution without touching code. Remember to back up your website before making any changes, especially when modifying the `functions.php` file. By following these guidelines, you can create a more focused and engaging shopping experience for your customers.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *