0

This code is from the archive page and list all products with a certain tag, can someone please help me to modify this code to make listing products only from current category:

<?php if ( is_product_tag() ) :?>   
    <?php do_action('woocommerce_before_shop_loop'); ?>
        <ul class="products">
            <?php while ( have_posts() ) : the_post(); ?>
                <?php woocommerce_get_template_part( 'content', 'product'); ?>
            <?php endwhile; // end of the loop. ?>
        </ul>
    <?php do_action('woocommerce_after_shop_loop'); ?>
<?php endif; ?>
  • WooCommerce already supports product listing by product category. No need to modify any code at all. – Steve Nov 6 '12 at 15:01
  • But I need a filter by category and tag, not only by category – Dime Nov 6 '12 at 15:14
  • Just to clarify. You want to show all products within a category that have a specific tag? – Steve Nov 6 '12 at 15:40
  • yes, can you help me? – Dime Nov 6 '12 at 15:52
0

Your best bet to do this would be to add a filter/hook on the specific tag. See this snippet at the WooCommerce API docs site on excluding a category for the proper format and to get going in the right direction. http://wcdocs.woothemes.com/snippets/exclude-a-category-from-the-shop-page/

But if you want a down and dirty way to filter for a specific tag on this content part then you can wrap an if then around your woocommerce_get_template_part().

<?php while ( have_posts() ) : the_post(); ?>
    <?php if ( *** product has tag *** ) { ?>
            <?php woocommerce_get_template_part( 'content', 'product'); ?>
    <?php endif; ?>
<?php endwhile; // end of the loop. ?>

You can look at the API docs on the product tags at http://wcdocs.woothemes.com/apidocs/class-WC_Product.html#_get_tags

Make sure you're following WooCommerce theme customization recommendations at http://wcdocs.woothemes.com/codex/template-structure/

Unless you want this to be global you'll have to account for the various outputs and determine when you don't want to filter both products and tags.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.