Skip to content

Instantly share code, notes, and snippets.

@Tusko
Last active June 10, 2016 06:44
Show Gist options
  • Save Tusko/f59cdcff574bd6a8bc51c7badca535f3 to your computer and use it in GitHub Desktop.
Save Tusko/f59cdcff574bd6a8bc51c7badca535f3 to your computer and use it in GitHub Desktop.
Alphabet Woocommerce Tags
<?php function alpha_brands(){
$num = range(0, 9);
$alpha = range('A','Z');
$abc_cyr = array();
foreach(range(chr(0xC0), chr(0xDF)) as $b) $abc_cyr[] = iconv('CP1251', 'UTF-8', $b);
$characters = array_merge($num, $alpha, $abc_cyr);
$result = array();
$t_args = array(
'hide_empty' => 0
);
$terms = get_terms('product_tag', $t_args);
ob_start();
if ( !empty( $terms ) && !is_wp_error( $terms ) ) {
foreach($characters as $val){
foreach($terms as $term){
if(stripos(strtolower($val), strtolower(mb_substr($term->name, 0, 1))) !== FALSE){
$result[$val][] = $term;
}
}
}
echo '<div class="shop_grid brands"><ul class="grid">';
foreach($result as $key => $trm) { ?>
<li class="grid-item" data-letter="<?php echo $key; ?>">
<div>
<?php foreach($trm as $t) { ?>
<a href="<?php echo get_term_link($t); ?>"><?php echo $t->name; ?></a>
<?php } ?>
</div>
</li>
<?php }
echo '</ul></div>';
}
$brands = ob_get_contents();
ob_clean();
return $brands;
}
add_shortcode('brands', 'alpha_brands');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment