Support » Fixing WordPress » 4.9 get_category_link() not working

  • Resolved priprod

    (@priprod)


    Hey All, I just upgraded to 4.9 and now my custom function for displaying links to categories isn’t working. Everything else is working except get_category_link. Here’s my code:

    $categories = get_terms('product_cat', array('parent' => $id));
    //add to the sublist
    foreach($categories as $c){
      $term_id = (int) $c->term_id;
      $link = get_category_link($term_id);
      ....

    Were there any updates to it that I’m not aware of?

    • This topic was modified 2 years, 2 months ago by priprod.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    In what way is it not working? According to the code, if $link is an empty string,then there category does not exist.

    https://core.trac.wordpress.org/browser/tags/4.9/src/wp-includes/category-template.php#L19

    Thank you, Steve.

    Prior to the update, the $link would return the actual link to the subcategory. After the update, it returned nothing.

    I removed the get_category_link and replaced it with get_term_link($term_id, ‘product_cat’); since I’m searching for subcategories in WooCommerce. This is now working as expected.

    Apparently get_category_link doesn’t work for this anymore, and get_term_link doesn’t work either in 4.9 without the second parameter.

    I’m seeing the same thing as you, @priprod.

    get_category_link($id) returns a string with a length of 0. This is with a known valid $id being passed to it.

    This worked in 4.8.3 and stopped working immediately after updating to 4.9 on multiple sites.

    EDIT: Replacing it with get_term_link($id) worked as expected.

    Thanks @priprod get_term_link($id) is now working as alternative of get_category_link($id)

    • This reply was modified 2 years, 1 month ago by waqas143.

    I had the same problem.

    Switching to get_term_link with a single argument for the term ID fixed it for me.

    If it makes any difference, I’m trying to get a link for a custom taxonomy and not the inbuilt post categories.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘4.9 get_category_link() not working’ is closed to new replies.