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 am using WooCommerce with SuperStore theme. In meta.php file is this line,

echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', $cat_count, 'woocommerce' ) . ' ', '.</span>' ); 

which displays the assigned categories to the post, but not in hierarchical order. I want to display these in hierarchical order.

I also found these piece of code, after some changes it is displaying the categories in correct order, but I need a new line tag if the top parent class is other then the previous category.

$taxonomy = 'product_cat';

    // get the term IDs assigned to post.
    $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
    // separator between links
    $separator = ' » ';

    if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {

        $term_ids = implode( ',' , $post_terms );
        $terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
        $terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );

        // display post categories
        echo  $terms;
    }

Example:

Shoes -Kids(sub)

Kids -Shoes(sub)

Now it is displayed as: Shoes » Kids » Kids » Shoes

I need it to display as:

Shoes » Kids

Kids » Shoes

Thank you for the help :)

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I have solved the issue with creating a list with

http://codex.wordpress.org/Template_Tags/wp_list_categories

changing style to list, then style it with CSS to my needs :)

<?php 

    $taxonomy = 'product_cat';

    // get the term IDs assigned to post.
    $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );

    if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {

        $term_ids = implode( ',' , $post_terms );

        $tophelp_categories = wp_list_categories( 'title_li=<span class="posted_in">Kategóriák:</span>&style=list&echo=0&depth=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );

        // display post categories
        echo ($tophelp_categories);
    }

?>
share|improve this answer

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.