仅获取父类别 wordpress

编程入门 行业动态 更新时间:2024-10-22 13:34:29
本文介绍了仅获取父类别 wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试创建一个类别列表,但我只想列出父类别而不是子类别.我怎样才能做到这一点?到目前为止,我已经创建了一个列表,其中列出了所有父类和子类.

i'm trying to create a category list, but i only want to list the parent categories and not the child categories. How can i do this? so far i've created a list, which list all parent and child categories.

function categoryList() {


  $args = array(
  'orderby' => 'name',
  'order' => 'ASC'
  );
$categories = get_categories($args);

  $output .= '<ul class="category-list">';
  foreach($categories as $category) { 
          if ($category){
          $output .= '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
          }
  } 

  $output .= '</li>';
  $output .= '</ul>';

  return $output;

}

推荐答案

对于父类别,我假设您指的是顶级类别.这实际上记录在 get_categories 的 Codex 页面上:您应该使用 parent => 调用 get_categories0

By parent categories, I assume you mean top-level categories. This is actually documented on the Codex page for get_categories: You should call get_categories with parent => 0

$args = array(
  'orderby' => 'name',
  'order' => 'ASC',
  'parent' => 0
);
$categories = get_categories($args);

这篇关于仅获取父类别 wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 05:53:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080237.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类别   wordpress

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!