【问题标题】:How to check if a category has a parent category?如何检查一个类别是否有父类别?
【发布时间】:2013-09-28 08:15:11
【问题描述】:

我正在尝试检查“categoryone”是否有父母。 知道我可以检查并看到有一个名为 categoryone 的类别,但如果 categoryone 有一个父类别,则不是。 我尝试编写类似下面的代码。

  $tid = term_exists('categoryone', 'category', 0);

  $term_ids = [];

  if ( $tid !== 0 && $tid !== null )
  {
$term_ids[] = $tid['term_id'];

  }
  else
  {
    // If there is not a parent category!
    $insert_term_id = wp_insert_term( 'categoryone', 'category' );
    if ( ! is_wp_error )
    $term_ids[] = $insert_term_id;
  }
  wp_set_post_categories( $insert_id, $term_ids );

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您可以使用这样的东西(将其粘贴到您的functions.php 文件中)

    function category_has_parent($catid){
        $category = get_category($catid);
        if ($category->category_parent > 0){
            return true;
        }
        return false;
    }
    

    从模板调用它

    if(category_has_parent($tid)) {
        // it has a parent
    }
    

    检查孩子

    function has_Children($cat_id)
    {
        $children = get_terms(
            'category',
            array( 'parent' => $cat_id, 'hide_empty' => false )
        );
        if ($children){
            return true;
        }
        return false
    }
    

    从模板调用它

    if(has_Children($tid)) {
        // it has children
    }
    

    【讨论】:

    • 不错的代码,但是是否可以检查 'categoryone' 是否有 Childe 类别?
    • 是的,当然,等等。
    【解决方案2】:

    您可以使用get_category() 通过传递term_id 获取当前类别详细信息

    $tid = term_exists('categoryone', 'category', 0);
    $t_details=get_category($tid);
    if(!empty($t_details->parent)){
    echo $t_details->parent; // parent category id or $t_details->category_parent
    }
    

    get_category() 返回如下对象,取自参考站点

    stdClass Object
    (
        [term_id] => 85
        [name] => Category Name
        [slug] => category-name
        [term_group] => 0
        [term_taxonomy_id] => 85
        [taxonomy] => category
        [description] => 
        [parent] => 70
        [count] => 0
        [cat_ID] => 85
        [category_count] => 0
        [category_description] => 
        [cat_name] => Category Name
        [category_nicename] => category-name
        [category_parent] => 70
    )
    

    编辑要获取子类别,您可以通过传递参数数组来使用get_categories()

    父母

    (整数)仅显示直接后代的类别(即 仅限儿童)由其 ID 标识的类别

    $args=array('parent'=>$tid);
    $child_categories=get_categories($args);
    

    【讨论】:

    • 超级喜欢你的代码!是否可以检查“categoryone”是否有 Childe 类别?
    【解决方案3】:

    您可以使用get_category_parents()(查看链接以阅读参数等)。这将按层次顺序返回所有父级的列表。在这种情况下,如果您只想要最直接的父类别,您可以这样做:

    <?php 
     $parent_cats = get_category_parents( $cat, false, ',' ); 
     $parent_cat = explode(",", $parent_cats); 
     echo $parent_cat[0];    
    ?>
    

    这将回显第一个父类别(直接在您当前类别上方的那个)。

    要明确:

    get_category_parents()
    - arg 1 是类别 ID(我只是随意使用了 $cat
    - arg 2 是如果您希望 wp 为每个返回的父类别添加链接
    - arg 3 是用于分隔返回类别的分隔符(如果有)

    explode()
    - arg 1 是用于分隔数组中字符串的分隔符
    - arg 2 是要分隔的源字符串

    编码愉快!

    【讨论】:

    • 很好,代码...是否可以检查 'categoryone' 是否有 Childe 类别?
    【解决方案4】:

    使用下面的代码来检查一个类别的父母和孩子。

    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // get current term
    
    $parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
    
    $children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children
    
    if(($parent->term_id!="" && sizeof($children)>0)) {
    
       // has parent and child
    
    }elseif(($parent->term_id!="") && (sizeof($children)==0)) {
    
       // has parent, no child
    
    }elseif(($parent->term_id=="") && (sizeof($children)>0)) {
    
       // no parent, has child
    
    }
    

    【讨论】:

      【解决方案5】:

      @M Khalid Junaid

      为了扩展您的答案,我使用了您的代码基础来检查该类别是否没有父项并且是类别/分类层次结构中的顶级项目。我将其包括在内,因为这是我偶然发现此线程时正在寻找的内容,也许其他人也在做同样的事情。

      <?php
      $args = array( 
          'taxonomy' = 'categories'; // If using custom post types, type in the custom post type's name
      );
      
      $terms = get_terms( $args );
      
      foreach ( $terms as $term ) {
          $has_parent = $term->parent;
          $name = $term->name;
      
          // If it doesn't have parents...
          if ( !$has_parent ) {
      
              // ...then it's the top tier in a hierarchy!
              echo "Tier 1:" $name . '<br />';
      
          }
      }
      ?>
      

      输出

      第 1 层:类别名称
      第 1 层:另一个类别名称
      第 1 层:所有顶级类别

      【讨论】:

        【解决方案6】:
        $queried = $wp_query->get_queried_object();
            if ($queried->category_parent) {
                // category has parent
            }
        

        【讨论】:

          【解决方案7】:

          首先,获取您的分类“taxonomy_name”下的所有术语和 然后检查他们每个人是否有父母。 试试下面的代码

          <?php
          $service_terms= get_terms( array(
              'taxonomy' => 'taxonomy_name'
          ) );
          ?>
          <?php if(!empty($service_terms)) : ?>
             <?php foreach($service_terms as $term) : ?>
                  <?php $has_parent = $term->parent; ?>
                  <?php if (!$has_parent): ?>
                                <!-- If this term is a parent then put your code here -->
                  <?php endif ?>
             <?php endforeach ?>
          <?php endif ?>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-07-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多