您需要使用分类查询。你可以在codex上阅读更多信息。我对您的代码进行了一些编辑。对于分页,我使用了上一个导航,下一个导航。
给这个代码试试,应该可以
<?php
/**
* The Template for displaying products in a product category. Simply includes the archive template.
*
* Override this template by copying it to yourtheme/woocommerce/taxonomy-product_cat.php
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
*/
if ( ! defined( \'ABSPATH\' ) ) {
exit; // Exit if accessed directly
}
get_header( \'shop\' ); ?>
<?php
/**
* woocommerce_before_main_content hook
*
* @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* @hooked woocommerce_breadcrumb - 20
*/
do_action( \'woocommerce_before_main_content\' );
?>
<div class="category-header">
<div class="cat-thumb"> <?php
// verify that this is a product category page
if (is_product_category()){
global $wp_query;
// get the query object
$cat = $wp_query->get_queried_object();
// get the thumbnail id user the term_id
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, \'thumbnail_id\', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );
// print the IMG HTML
echo \'<img src="\'.$image.\'" alt="" />\';
}
?></div>
<?php if ( apply_filters( \'woocommerce_show_page_title\', true ) ) : ?>
<h1 class="page-title"> <?php woocommerce_page_title(); ?></h1>
<?php endif; ?>
<?php
/**
* woocommerce_archive_description hook
*
* @hooked woocommerce_taxonomy_archive_description - 10
* @hooked woocommerce_product_archive_description - 10
*/
do_action( \'woocommerce_archive_description\' );
?>
</div>
<ul class="cat-products">
<?php
$paged= (get_query_var(\'paged\' )) ? get_query_var(\'paged\'):1;
$args = array(
\'post_type\' => \'product\',
\'paged\' => $paged,
\'posts_per_page\' => 5,
\'tax_query\' => array(
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'id\',
\'terms\' => $cat->term_id,
),
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li <?php post_class( $classes ); ?>>
<?php do_action( \'woocommerce_before_shop_loop_item\' ); ?>
<a href="<?php the_permalink(); ?>">
<?php
/**
* woocommerce_before_shop_loop_item_title hook
*
* @hooked woocommerce_show_product_loop_sale_flash - 10
* @hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( \'woocommerce_before_shop_loop_item_title\' );
/**
* woocommerce_shop_loop_item_title hook
*
* @hooked woocommerce_template_loop_product_title - 10
*/
do_action( \'woocommerce_shop_loop_item_title\' );
/**
* woocommerce_after_shop_loop_item_title hook
*
* @hooked woocommerce_template_loop_rating - 5
* @hooked woocommerce_template_loop_price - 10
*/
do_action( \'woocommerce_after_shop_loop_item_title\' );
?>
<p> </p>
<div class="product-desp"> <?php
echo wp_trim_words( get_the_content(), 27, \'...\' );
?></div>
</a>
<?php
/**
* woocommerce_after_shop_loop_item hook
*
* @hooked woocommerce_template_loop_add_to_cart - 10
*/
do_action( \'woocommerce_after_shop_loop_item\' );
?>
</li>
<?php
endwhile;
} else {
echo __( \'No products found\' );
}
wp_reset_postdata();
?>
</ul>
<div class="pagination">
<p class="pull-left previous" ><?php previous_posts_link( \'← Previous \', $loop->max_num_pages ); ?></p>
<p class="pull-right next" ><?php next_posts_link( \'More →\', $loop->max_num_pages ); ?></p>
</div>
<?php
/**
* woocommerce_after_main_content hook
*
* @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action( \'woocommerce_after_main_content\' );
?>
<?php get_footer( \'shop\' ); ?>