How to check if a category has a parent category?

I am trying to check if category categoryone has a parent element. I know correctly that I can check and see that there is a category called categoryone, but not if categoryone has a parent category. I tried to code something like the code below.

$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 ); 
+6
source share
6 answers

You can use something like this (Insert this into your functions.php file)

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

Call this from a template

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

Check children

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

Call this from a template

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

You can use get_category () to get information about the current category by passing 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 () returns an object, as shown below, from the link site

 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 ) 

EDIT . To get child categories, you can use get_categories () by passing an array of arguments.

parental

(integer) Display only those categories that are direct descendants (i.e. only children) of the category identified by its identifier

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

Well, you can use get_category_parents() (see link to read arguments, etc.). This will return a list of all parents in a hierarchical order. In this case, if you just need the most immediate parent category, you can do something like this:

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

This will reflect the first parent category (the one immediately above your current category).

To be clear:

in get_category_parents()
- arg 1 - category id (I just used $cat arbitrarily)
- arg 2 - this is if you want wp to add a link to each returned parent category
- arg 3 - separator used to separate returned categories, if any

in explode()
- arg 1 is the separator to look for to split the string in the array
- arg 2 is the source string to split

Happy coding!

+1
source

Use the code below to check the parent and child categories.

 $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 } 
+1
source

@M Khalid Junaid

To expand on my answer, I used the base of your code to check if this category has parents and whether there is a top-level element in the hierarchical hierarchy of category / taxonomy. I turn it on because it is what I was looking for when I came across this topic and maybe someone else is doing the same.

 <?php $args = array( 'taxonomy' = 'categories'; // If using custom post types, type in the custom post type 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 the top tier in a hierarchy! echo "Tier 1:" $name . '<br />'; } } ?> 

Output

Level 1: Category Name
Level 1: another category name
Level 1: All top level categories.

0
source
 $queried = $wp_query->get_queried_object(); if ($queried->category_parent) { // category has parent } 
0
source

Source: https://habr.com/ru/post/954829/


All Articles