WooCommerce's product categories are a custom taxonomy called product_cat. In a function I'm writing, I'm using get_categories with the taxonomy parameter set to product_cat. Everything works fine and I can get the term id, the name, and even the slug. What I can't figure out is how to get the link to display. Apparently get_category_link doesn't work with custom taxonomy and get_term_link isn't working either, I get an error. Here's what I have:

$prod_cat_args = array(
  'taxonomy'     => 'product_cat', //woocommerce
  'orderby'      => 'name',
  'empty'        => 0
);

$woo_categories = get_categories( $prod_cat_args );

foreach ( $woo_categories as $woo_cat ) {
    $woo_cat_id = $woo_cat->term_id; //category ID
    $woo_cat_name = $woo_cat->name; //category name

    $return .= '<a href="' . get_category_link( $woo_cat_id ) . '">' . $woo_cat_name . '</a>';
}//end of $woo_categories foreach  

Suggestions?

up vote 11 down vote accepted

Another update (Sept. 2015):

I can use get_term_link after all. The problem was that the string needed to be converted to an integer. Used a Stack Overflow tip for the fastest way to convert it using the (int)$value in PHP.

So it would look like this if you don't want to use the slug in the foreach loop:

$woo_cat_id_int = (int)$woo_cat_id; //convert 

That converted value is used instead of the slug in get_term_link. Hope it helps someone. :-)


Looks like I figured it out.

I used get_term_link. And I was getting an error because I was using it this way:

get_term_link( $woo_cat_id, 'product_cat' );

Which gave me this error:

Object of class WP_Error could not be converted to string

So I went this route instead with the slug and it worked:

$prod_cat_args = array(
  'taxonomy'     => 'product_cat', //woocommerce
  'orderby'      => 'name',
  'empty'        => 0
);

$woo_categories = get_categories( $prod_cat_args );

foreach ( $woo_categories as $woo_cat ) {
    $woo_cat_id = $woo_cat->term_id; //category ID
    $woo_cat_name = $woo_cat->name; //category name
    $woo_cat_slug = $woo_cat->slug; //category slug


    $return .= '<a href="' . get_term_link( $woo_cat_slug, 'product_cat' ) . '">' . $woo_cat_name . '</a>';
}//end of $woo_categories foreach  
  • 1
    Although I still don't understand why it won't take the ID but it takes the slug. The Codex says get_term_link should take the ID... – RachieVee Oct 3 '14 at 18:28
  • 1
    Makes zero sense - should work with the id indeed... many thanks – alemur Nov 19 '14 at 10:46
  • 3
    Term_id is a string on the object. To use it with the get term link function you need to parse it as an integer first get_term_link( intval($woo_cat->term_id), 'product_cat' ) – forsvunnet Jan 28 '15 at 14:54
  • The solution by forsvunnet woked perfectly for me – Shane Jones Aug 31 '16 at 22:34

Thanks, I use

foreach ( $terms as $term ) {
$term_link = get_term_link( $term );
 echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
}

It works perfectly.

$prod_cat_args = array(
'taxonomy'     => 'product_cat', //woocommerce
            'orderby'      => 'name',
            'empty'        => 0
            );

            $terms = get_categories( $prod_cat_args );
            //$term_id=6;
            foreach ( $terms as $term ) {
            $term_link = get_term_link( $term );
            echo '<li><a class="shopping-now" href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
            }

get_term_link() does work smoothly, when using the object returned by get_categories().

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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