Last active
December 20, 2015 07:39
-
-
Save energy0m/6095177 to your computer and use it in GitHub Desktop.
This is an attempt at creating a woocommerce product category shortcode for the Neighborhood theme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function my_cats( $atts ) { | |
global $woocommerce_loop; | |
global $my_cats; | |
extract( shortcode_atts( array ( | |
'ids' => '', | |
'orderby' => 'name', | |
'order' => 'DESC', | |
'columns' => '4', | |
'hide_empty' => 1, | |
'parent' => '' | |
), $atts ) ); | |
if ( isset( $atts[ 'ids' ] ) ) { | |
$ids = explode( ',', $atts[ 'ids' ] ); | |
$ids = array_map( 'trim', $ids ); | |
} else { | |
$ids = array(); | |
} | |
$hide_empty = ( $hide_empty == true || $hide_empty == 1 ) ? 1 : 0; | |
// get terms and workaround WP bug with parents/pad counts | |
$args = array( | |
'orderby' => $orderby, | |
'order' => $order, | |
'hide_empty' => $hide_empty, | |
'include' => $ids, | |
'pad_counts' => true, | |
'child_of' => $parent | |
); | |
#var_dump ($args); | |
$my_cats = get_terms( 'product_cat', $args ); | |
if ( $parent !== "" ) | |
$my_cats = wp_list_filter( $my_cats, array( 'parent' => $parent ) ); | |
if ( $number ) | |
$my_cats = array_slice( $my_cats, 0, $number ); | |
$woocommerce_loop['columns'] = $columns; | |
ob_start(); | |
// Reset loop/columns globals when starting a new loop | |
$woocommerce_loop['loop'] = $woocommerce_loop['column'] = ''; | |
if ( $my_cats ) { | |
woocommerce_product_loop_start(); | |
foreach ( $my_cats as $category ) { | |
woocommerce_get_template( 'product-output.php', array( | |
'category' => $category | |
) ); | |
} | |
woocommerce_product_loop_end(); | |
} | |
woocommerce_reset_loop(); | |
return '<div class="row"> <div class="product_list_widget products-mini woocommerce spb_content_element span12"><div class="spb_wrapper"><h4 class="spb_heading"><span>Featured Categories</span></h4> | |
<div class="product-carousel" data-columns="6"><div class="carousel-overflow"> | |
' . ob_get_clean() . '</div><a href="#" class="prev"><i class="icon-chevron-left"></i></a><a href="#" class="next"><i class="icon-chevron-right"></i></a></div></div></div></div>'; | |
} | |
add_shortcode( 'mycats', 'my_cats' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment