开发者

get_category_link() is returning nothing

I am using the get_categories() function to manually create myself a nav menu. I have a custom taxonomy I'm using called Category and I'm trying to return the link for it for my tags in the menu using the get_category_link() function.

foreach ($categories as $category) {
            if ($category->parent == 0) {   //Check to see it is a parent
                $output .= '<li>';
                $output .= '<a href="' . get_category_link($category->cat_ID) . '">' . $category-开发者_StackOverflow社区>name . '</a>';  //display parent taxonomy category

             }
      }

But it always returns <a href="">. I can echo out the $category->cat_ID successfully so I know it is passing the ID into the function but I don't know why it's returning blank.

Am I missing something? Is it because these are custom taxonomies? They have slugs.


You need something like this for custom taxonomies:

$tax = 'cars';
  $cats = get_terms( $tax, '' );
  if ($cats) {
    foreach($cats as $cat) {
       $output .= "<li>";
$output .= '<a href="' . esc_attr(get_term_link($cat, $tax)) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a>';
$output .= "</li>";
    }
  }

Although you can easily add to the top of the script to get an array of all taxonomies to feed in if you wanted.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜