php – 如何检查类别是否具有父类别?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何检查类别是否具有父类别?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图检查’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 );
你可以使用这样的东西(在你的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
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何检查类别是否具有父类别?全部内容,希望文章能够帮你解决php – 如何检查类别是否具有父类别?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。