Skip to content

Instantly share code, notes, and snippets.

@davideperrotta
Forked from corsonr/gist:5933479
Last active March 17, 2018 16:44
Show Gist options
  • Save davideperrotta/7a26e1f8525ac65a05b8b6630d449a02 to your computer and use it in GitHub Desktop.
Save davideperrotta/7a26e1f8525ac65a05b8b6630d449a02 to your computer and use it in GitHub Desktop.
List WooCommerce products by tags
<?php
/**
* Plugin Name: WooCommerce - List Products by Tags
* Plugin URI: http://www.remicorson.com/list-woocommerce-products-by-tags/
* Description: List WooCommerce products by tags using a shortcode, ex: [woo_products_by_tags tags="shoes,socks"]
* Version: 1.0
* Author: Remi Corson
* Contributor: Davide Perrotta - http://www.davideperrotta.it
* Author URI: http://remicorson.com
* Requires at least: 3.5
* Tested up to: 4.5
*
*/
/*
* List WooCommerce Products by tags
*
* ex: [woo_products_by_tags tags="shoes,socks"]
*/
function woo_products_by_tags_shortcode( $atts, $content = null ) {
// Get attribuets
extract(shortcode_atts(array(
"tags" => ''
), $atts));
ob_start();
// Define Query Arguments
$args = array(
'post_type' => 'product',
'posts_per_page' => 5,
'product_tag' => $tags
);
// Create the new query
$loop = new WP_Query( $args );
// Get products number
$product_count = $loop->post_count;
// If results
if( $product_count > 0 ) :
echo '<ul class="products">';
// Start the loop
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
global $post;
echo "<a href='".get_permalink( $query->post->ID )."'><li class='product'>" . $thePostID = $post->post_title;
if (has_post_thumbnail( $loop->post->ID ))
echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
else
echo '<img src="'.$woocommerce->plugin_url().'/assets/images/placeholder.png" alt="" width="'.$woocommerce->get_image_size('shop_catalog_image_width').'px" height="'.$woocommerce->get_image_size('shop_catalog_image_height').'px" /></li></a>';
endwhile;
echo '</ul><!--/.products-->';
else :
_e('No product matching your criteria.');
endif; // endif $product_count > 0
return ob_get_clean();
}
add_shortcode("woo_products_by_tags", "woo_products_by_tags_shortcode");
@davideperrotta
Copy link
Author

davideperrotta commented May 25, 2016

I've made some changes that allow to click and styling products like WooCommerce do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment