Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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:

product_cat
    |
    |––cat1  
    |   |––child1.1  
    |   |––child1.2  
    |   |––child1.3  
    |
    |––cat2  
        |––child2.1  
        |––child2.2  
        |––child2.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.

share|improve this answer
    
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/priveeko‌​llektie/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 –  ialocin Oct 5 '13 at 9:21
    
Thanks! It's working. –  tjwillems Oct 7 '13 at 14:58
    
@tjwillems Any time! If it's a correct answer give it a check mark, so everybody coming to the question later knows that. –  ialocin 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 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.

share|improve this answer
    
Just hiding the element isn't optimal and using inline style declaration should be avoided. –  ialocin Oct 4 '13 at 13:12
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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