Created
June 27, 2014 00:47
-
-
Save patrickgilmour/86ceaa448c481435eaad to your computer and use it in GitHub Desktop.
WooCommerce - show a product's single child category (term) from a defined Parent term in the WooCommerce Products taxonomy, 'product_cat'. Works on the Single Product Page.
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 | |
/** | |
* Show a Product's Child Category of a Parent | |
* | |
* For example, A parent called "Brand" and a child called "Apple" | |
*/ | |
add_action('woocommerce_after_single_product', 'my_woocommerce_after_single_product' ); | |
function my_woocommerce_after_single_product () { | |
global $product; | |
$taxonomy = 'product_cat'; | |
$slug = 'brand'; //Or whatever the slug of "Brand" is | |
$term = get_term_by('slug', $slug, $taxonomy); | |
//Gets the ID of the Parent category | |
$term_id = $term->term_id; | |
//Get the Child terms | |
$brand_terms = wp_get_post_terms( $product->id, $taxonomy, array("fields" => "all") ); | |
foreach ($brand_terms as $brand_item) { | |
// Hunt out the child term that is a child of the parent | |
if ( $brand_item->parent == $term_id ) { | |
//Get the name of the brand | |
$brand = $brand_item->name; | |
break; //Assumes you've only assigned one Brand | |
} | |
} | |
echo '<p>The Brand is: ' . $brand; //Apple or Microsoft or Google... | |
} | |
// see http://stackoverflow.com/questions/24033625/show-brand-on-woocommerce-product-page/24442399#24442399 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment