Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Created January 22, 2013 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpsmith/4592102 to your computer and use it in GitHub Desktop.
Save wpsmith/4592102 to your computer and use it in GitHub Desktop.
<?php
/**
* Outputs HTML markup for category image for WooCommerce.
*
* @param int $cat_id Category ID.
* @param string $size Image size.
* @param array $attr Image attributes.
* @return string $orderby Modified orderby SQL string.
*/
function wps_category_image( $cat_id, $size, $attr = array() ) {
// get the thumbnail id user the term_id
$thumbnail_id = get_woocommerce_term_meta( $cat_id, 'thumbnail_id', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );
// print the IMG HTML
echo wp_get_attachment_image( $thumbnail_id, $size, false, $attr );
}
<?php
add_filter( 'get_terms_orderby', 'wps_get_terms_orderby', 10, 2 );
/**
* Modifies the get_terms_orderby argument if orderby == include
*
* @param string $orderby Default orderby SQL string.
* @param array $args get_terms( $taxonomy, $args ) arg.
* @return string $orderby Modified orderby SQL string.
*/
function wps_get_terms_orderby( $orderby, $args ) {
if ( isset( $args['orderby'] ) && 'include' == $args['orderby'] ) {
$include = implode(',', array_map( 'absint', $args['include'] ));
$orderby = "FIELD( t.term_id, $include )";
}
return $orderby;
}
<?php
/**
* Output the homepage categories HTML Markup
*
* @uses wps_category_image() Outputs image HTML Markup
*/
function wps_homepage_categories() {
$args = array(
'orderby' => 'include',
'order' => 'ASC',
'hide_empty' => false,
'include' => array( 28 /* Appearal */, 27 /* Accessories */, 30 /* Outerwear */, 29 /* Hiking */, 22 /* Grooming */, ),
'fields' => 'all',
'pad_counts' => false,
'd2c_home' => true, //optional
);
$product_cats = get_terms( 'product_cat', $args );
foreach ( $product_cats as $cat ) {
wps_category_image( $cat->term_id, $size );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment