I want to edit the following piece of code:

<?php if ( (!$theretailer_theme_options['category_listing']) || ($theretailer_theme_options['category_listing'] == 0) ) { ?>
        <!-- Show only the first category-->
        <?php $gbtr_product_cats = strip_tags($product->get_categories('|||', '', '')); //Categories without links separeted by ||| ?>
        <h3><a href="<?php the_permalink(); ?>"><?php list($firstpart) = explode('|||', $gbtr_product_cats); echo $firstpart; ?></a></h3>
        <?php } ?>

Example: http://tjwillems.nl/priveekollektie/product-category/artists/reinier-bosch/

The red-title under the product image only needs to show the child categories of 'artist'. So for example only 'Reinier Bosch' and not 'Lighting'

Can somebody help me?

Greetz, Tom


The woocommerce function get_categories() is declared in abstract-wc-product.php, because it is based on the wordpress function get_the_term_list() there is no way to get only a specific branch of a category. This absolutely isn't the same as the wordpress function get_categories(), you can see that it is woocommerce specific by how it's used $product->get_categories(). Besides the obvious difference in arguments you can use of course, you can get deeper into that reading and comparing the linked information.

exemplarily categoryTaxonomy tree:

    |   |––child1.1  
    |   |––child1.2  
    |   |––child1.3  

In your case branches would be »artist« and »art«, instead of »cat1« and »cat2«. The goal is to get only the one relevant category branch for the title you want to show. At least this is what the below code is going to achieve.

Like I said above, if you want to achieve getting only terms from a specific branch of a category/taxonomy you have to do it a different way. You have to make use of get_terms() and wp_get_post_terms() and then intersect the results.

//woocommerce product category
$taxonomies = array( 'product_cat' );

//let's get an array with ids of all the terms a post has  
$post_tids = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "ids"));

//you have to change branch parent id accordingly
$id_branch_parent = '111';
$args = array(
    'fields' => 'ids',
    'child_of' => $id_branch_parent
//let's get an array with ids of all the terms a the branch has  
$branch_tids = get_terms( $taxonomies, $args );

//intersect post and branch terms to get an array of common ids
$pnb_tids = array_intersect($post_tids, $branch_tids);

//in case of multiple ids we have to loop through them
foreach ( $pnb_tids as $tid ) {
    //get the term information by id
    $tobj = get_term_by('id', $tid, 'product_cat');
    //store the term names into an array
    $pnb_name_arr[] = $tobj->name;

//implode the name array, this is the result you want to echo
$pnb_term_list = implode(' – ', $pnb_name_arr);

This should give you the result you want, but it's untested.

  • Thanks for your response! I think it needs a little tweak before it will work.. Now I got this fatal error... PHP is not my specialty :-( Fatal error: Cannot use object of type stdClass as array in /var/www/vhosts/tjwillems.nl/httpdocs/priveekollektie/wp-content/themes/priveekollektie/woocommerce/content-product.php on line 111 This line of code is on line 111 $pnb_name_arr[] = $tobj['name'];
    – tjwillems
    Oct 4 '13 at 22:46
  • @tjwillems: I changed the code/line
    – Nicolai
    Oct 5 '13 at 9:21
  • @tjwillems Any time! If it's a correct answer give it a check mark, so everybody coming to the question later knows that.
    – Nicolai
    Oct 8 '13 at 14:29
  • @ialocin, thanks a lot! This answer helped me. Just a question, how do I get the URL to the child category?
    – input
    Mar 11 '14 at 22:12

You can comment out this part

    <h3><a href="<?php the_permalink(); ?>"><?php list($firstpart) = explode('|||', $gbtr_product_cats); echo $firstpart; ?></a></h3>

Add to the html ....

Also you can use CSS to hide the h3 for example

<style> .product_item h3 {display: none !important;} </style>

You can add to your css.

Doing that the 'red' text will disappear, and u only will have the other text.

  • 1
    Just hiding the element isn't optimal and using inline style declaration should be avoided.
    – Nicolai
    Oct 4 '13 at 13:12
  • 1
    It's not that I want to hide the H3 titles. I want to change the function. Now it is displaying the selected category by alphabet. But I want it to show the child categories of a specific parent... (hard to explain :) )
    – tjwillems
    Oct 4 '13 at 14:41

Here is the simplest way to display product child categories from specific parent id

$parent_id = 12;    // parent category id
// Get child categories from parent id
$get_child_category = get_terms('product_cat',array('child_of' => $parent_id));
// Filter products from child category
foreach($get_child_category as $child_category){
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => 10,
        'product_cat' => $child_category->slug,
        'order' => 'DESC',

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.