简体   繁体   English

如何在WordPress中获取父类别ID

[英]how to get parent category id in wordpress

how to get only parent category ids. 如何仅获取父类别ID。 not children category ids i tried this code before which is showing me all ids of categories 不是儿童类别ID,我在尝试此代码之前向我显示了所有类别ID

 <?php  $category_ids = get_all_category_ids();

 foreach($category_ids as $cat_id) {
  $cat_name = get_cat_name($cat_id);
  //echo '<span class="png_bg category_icon"></span>' . $cat_name ;
?>
            <option><?php echo '<span class="png_bg category_icon"></span>' . $cat_name ; ?></option>
   <?php
 }
   ?>          
            </select>

There can be done in a number of ways. 可以通过多种方式来完成。 One of them is 其中之一是

$categories = get_categories();
foreach ($categories as $cat)
{
  // if it is a topmost category , it has no parents, ie parent=0
  if($cat->parent < 1)
  {
    echo $cat->category_nicename
    echo $cat->cat_name ;
   }
}

Here is the function: 这是函数:

<?php get_category_parents($cat); ?>

This function accepts several arguments, you can find the full reference on wordpress codex 该函数接受多个参数,您可以在wordpress Codex上找到完整的参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM