Skip to content

Instantly share code, notes, and snippets.

@MortenAndersen
Forked from ChromeOrange/product-tag.php
Last active September 25, 2015 15:22
Show Gist options
  • Select an option

  • Save MortenAndersen/2cda4918ef1b9db7f12e to your computer and use it in GitHub Desktop.

Select an option

Save MortenAndersen/2cda4918ef1b9db7f12e to your computer and use it in GitHub Desktop.

Revisions

  1. MortenAndersen revised this gist Sep 25, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions product-tag.php
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,9 @@ function woocommerce_product_tag( $atts ){
    if ( empty( $atts ) ) return;

    extract( shortcode_atts( array(
    'per_page' => '12',
    'per_page' => '4',
    'columns' => '4',
    'orderby' => 'title',
    'orderby' => 'rand',
    'order' => 'desc',
    'tag' => ''
    ), $atts ) );
  2. @ChromeOrange ChromeOrange revised this gist Apr 3, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion product-tag.php
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ function woocommerce_product_tag( $atts ){
    'tag' => ''
    ), $atts ) );

    if ( ! $category ) return;
    if ( ! $tag ) return;

    // Default ordering args
    $ordering_args = $woocommerce->query->get_catalog_ordering_args( $orderby, $order );
  3. @ChromeOrange ChromeOrange revised this gist Mar 31, 2013. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions product-tag.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    /**
    * List products in a product tag shortcode
    * List products in a product tag shortcode
    * Useage : [product_tag tag="foo"]
    */
    */
    add_shortcode( 'product_tag', 'woocommerce_product_tag' );
    function woocommerce_product_tag( $atts ){
    global $woocommerce, $woocommerce_loop;
    @@ -11,8 +11,8 @@ function woocommerce_product_tag( $atts ){
    extract( shortcode_atts( array(
    'per_page' => '12',
    'columns' => '4',
    'orderby' => 'title',
    'order' => 'desc',
    'orderby' => 'title',
    'order' => 'desc',
    'tag' => ''
    ), $atts ) );

    @@ -22,11 +22,11 @@ function woocommerce_product_tag( $atts ){
    $ordering_args = $woocommerce->query->get_catalog_ordering_args( $orderby, $order );

    $args = array(
    'post_type' => 'product',
    'post_type' => 'product',
    'post_status' => 'publish',
    'ignore_sticky_posts' => 1,
    'orderby' => $ordering_args['orderby'],
    'order' => $ordering_args['order'],
    'ignore_sticky_posts' => 1,
    'orderby' => $ordering_args['orderby'],
    'order' => $ordering_args['order'],
    'posts_per_page' => $per_page,
    'meta_query' => array(
    array(
    @@ -38,9 +38,9 @@ function woocommerce_product_tag( $atts ){
    'tax_query' => array(
    array(
    'taxonomy' => 'product_tag',
    'terms' => array( esc_attr($tag) ),
    'field' => 'slug',
    'operator' => 'IN'
    'terms' => array( esc_attr($tag) ),
    'field' => 'slug',
    'operator' => 'IN'
    )
    )
    );
  4. @ChromeOrange ChromeOrange created this gist Mar 31, 2013.
    75 changes: 75 additions & 0 deletions product-tag.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    /**
    * List products in a product tag shortcode
    * Useage : [product_tag tag="foo"]
    */
    add_shortcode( 'product_tag', 'woocommerce_product_tag' );
    function woocommerce_product_tag( $atts ){
    global $woocommerce, $woocommerce_loop;

    if ( empty( $atts ) ) return;

    extract( shortcode_atts( array(
    'per_page' => '12',
    'columns' => '4',
    'orderby' => 'title',
    'order' => 'desc',
    'tag' => ''
    ), $atts ) );

    if ( ! $category ) return;

    // Default ordering args
    $ordering_args = $woocommerce->query->get_catalog_ordering_args( $orderby, $order );

    $args = array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'ignore_sticky_posts' => 1,
    'orderby' => $ordering_args['orderby'],
    'order' => $ordering_args['order'],
    'posts_per_page' => $per_page,
    'meta_query' => array(
    array(
    'key' => '_visibility',
    'value' => array('catalog', 'visible'),
    'compare' => 'IN'
    )
    ),
    'tax_query' => array(
    array(
    'taxonomy' => 'product_tag',
    'terms' => array( esc_attr($tag) ),
    'field' => 'slug',
    'operator' => 'IN'
    )
    )
    );

    if ( isset( $ordering_args['meta_key'] ) ) {
    $args['meta_key'] = $ordering_args['meta_key'];
    }

    ob_start();

    $products = new WP_Query( $args );

    $woocommerce_loop['columns'] = $columns;

    if ( $products->have_posts() ) : ?>

    <?php woocommerce_product_loop_start(); ?>

    <?php while ( $products->have_posts() ) : $products->the_post(); ?>

    <?php woocommerce_get_template_part( 'content', 'product' ); ?>

    <?php endwhile; // end of the loop. ?>

    <?php woocommerce_product_loop_end(); ?>

    <?php endif;

    wp_reset_postdata();

    return '<div class="woocommerce">' . ob_get_clean() . '</div>';
    }