加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 13:04:12 所属栏目:PHP教程 来源:网络整理
导读:我试图检查’categoryone’是否有父母. 知道我可以检查并看到有一个名为categoryone的类别,但如果categoryone有父类别则不行. 我试图编写类似下面代码的代码. $tid = term_exists('categoryone','category',0); $term_ids = []; if ( $tid !== 0 $tid !== nu
我试图检查’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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读