WordPress   

Custom template for product categories archive with numbered pagination

This template is a minimal archive template for displaying woocommerce product category pages. It can be easily customized to increase or decrease the current features in it.

<?php
/**
 * Custom template for product categories
 */

get_header();
$description = get_the_archive_description();
global $product;

function getProductPrice($product){
	if( $product->is_on_sale() ) {
		$productPrice .= "<s>".wc_price($product->get_regular_price())."</s>";
		$productPrice .= wc_price($product->get_sale_price());
	}
	else{ 
		$productPrice = wc_price($product->get_regular_price()); 
	}
	return $productPrice;
}
?>
<div class="archive-header">
	<h1 class="page-title"><?php the_archive_title(); ?></h1>
	<p><?php echo $description; ?></p>
</div>
<div class="products-wrap">
	<?php while ( have_posts() ) : the_post(); 
	$product = wc_get_product( get_the_ID() ); ?>
	<div class="product-box">
		<?php echo get_the_post_thumbnail(); ?>
		<h3 class="product-name"><?php echo get_the_title(); ?></h3>
		<div>
			<?php 
				echo getProductPrice($product);
			?>
		</div>
		<a href="<?php echo $product->add_to_cart_url() ?>" value="<?php echo esc_attr( $product->get_id() ); ?>" class="ajax_add_to_cart add_to_cart_button" data-product_id="<?php echo get_the_ID(); ?>" data-product_sku="<?php echo esc_attr($sku) ?>" aria-label="Add “<?php the_title_attribute() ?>” to your cart"> Add to Cart </a>
		<a href="<?php the_permalink(); ?>">View Product</a>
	</div>
	<?php endwhile; ?>
<div class="pagination">
	<?php echo paginate_links( $args ) ?>
</div>
<?php wp_reset_postdata(); ?>
</div>
<?php get_footer(); ?>

How to change the number of products in the template pagination?

The total number of products can be modified easily using pre_get_posts . Paste the following code snippet into the functions.php file and modify the total number of products according to your requirements

function modify_total_products_on_archive($query) {
	if (!is_admin() && $query->is_main_query() && is_product_category() ) 
		$query->query_vars['posts_per_page'] = 5; 
	return $query; 
}
add_filter('pre_get_posts', 'modify_total_products_on_archive'); 
Need Professional Support for Your Website Problems?

Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.

Hire Us