Change Number of Products Displayed on WooCommerce Store Page
Explanation
If you're looking to adjust how many products show up on your WooCommerce store page, this snippet is your go-to solution.
What It Does:
- Changes the number of products displayed on your shop, category, or tag pages.
- Works by modifying the main query that determines what products are shown.
How to Use It:
- Find the line that says
$query->set( 'posts_per_page', 12 );
. - Change the number
12
to however many products you want to display per page.
This code is hooked into the pre_get_posts action, which means it runs before WordPress fetches the products to display. It checks if you're on a shop, category, or tag page and then sets the number of products accordingly.
Remember, this change will affect all shop-related pages, so make sure the number you choose fits well with your site's design and user experience.
Code
<?php
// Function to change the number of products displayed per page in WooCommerce
function wp_dudecom_change_products_per_page( $query ) {
if ( is_shop() || is_product_category() || is_product_tag() ) {
// Check if it's the main query
if ( $query->is_main_query() ) {
// Set the number of products per page
$query->set( 'posts_per_page', 12 ); // Change 12 to the desired number of products per page
}
}
}
add_action( 'pre_get_posts', 'wp_dudecom_change_products_per_page' );
?>
Instructions
File Location: Add the following code to your theme's functions.php
file or a custom plugin file if you prefer to keep theme modifications separate.
Prerequisites:
- Ensure WooCommerce is installed and activated on your WordPress site.
Implementation Steps:
- Log in to your WordPress admin dashboard.
- Navigate to Appearance > Theme Editor if you are editing the
functions.php
file, or go to Plugins > Editor if you are using a custom plugin. - Locate and open the
functions.php
file or your custom plugin file. - Copy and paste the provided code snippet into the file.
- Find the line
$query->set( 'posts_per_page', 12 );
and change12
to the desired number of products you want to display per page. - Save the changes to the file.
- Visit your WooCommerce shop page to verify that the number of products displayed per page has been updated.
If you need assistance with this implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert WordPress support.