8

我试图检查'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 );
4

7 回答 7

24

您可以使用类似的东西(将其粘贴到您的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
}
于 2013-09-28T09:20:59.273 回答
3

您可以使用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);
于 2013-09-28T09:21:32.257 回答
1

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

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

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

要清楚:

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

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

快乐编码!

于 2013-09-28T09:15:38.267 回答
1

使用以下代码检查类别的父项和子项。

$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

}
于 2015-10-14T10:01:40.373 回答
0

首先,获取您的分类法“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 ?>
于 2019-12-31T09:51:11.327 回答
0

@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 层:所有顶级类别

于 2017-05-23T06:37:40.600 回答
0
$queried = $wp_query->get_queried_object();
    if ($queried->category_parent) {
        // category has parent
    }
于 2018-03-06T17:58:53.270 回答