get_category_link() 不返回任何内容

发布于 2024-11-14 08:56:14 字数 657 浏览 7 评论 0原文

我正在使用 get_categories() 函数为自己手动创建一个导航菜单。我正在使用一个名为 Category 的自定义分类法,并且我尝试使用 get_category_link() 函数返回菜单中标签的链接。

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->name . '</a>';  //display parent taxonomy category

             }
      }

但它总是返回 。我可以成功回显 $category->cat_ID,因此我知道它将 ID 传递到函数中,但我不知道为什么它返回空白。

我错过了什么吗?是因为这些是自定义分类法吗?他们有蛞蝓。

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->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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2024-11-21 08:56:14

对于自定义分类法,您需要类似的内容:

$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>";
    }
  }

尽管您可以轻松添加到脚本顶部以获取要输入的所有分类法的数组(如果您愿意)。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文