Tutorial :Check if_parent and if_child in Wordpress



Question:

I need to create a function for WP that will check if the current category is both child of X category and parent to Z category.

Ideas?


Solution:1

Untested MySQL query to get you started:

SELECT x.cat_ID, y.cat_ID, z.cat_ID  FROM   categories y,      LEFT JOIN categories x ON y.parent = x.cat_ID,      LEFT JOIN categories z ON z.parent = y.cat_ID,  WHERE  y.cat_name = ? AND         x.cat_name = ? AND         z.cat_name = ?  


Solution:2

Try this:

<?php  $catid = get_query_var('cat');  if (cat_is_ancestor_of($catid,$test_child_cat) && cat_is_ancestor_of($test_parent_cat,$catid)) {  echo "Current Category is child of X and Parent of Y";  }  ?>  


Solution:3

I finally got to write this simple solution for getting both Parent and Children form the current viewed category in Wordpress:

    $children = $wp_query->query_vars[category__in];    $count = 0;      echo 'Parent: ' . $wp_query->queried_object->parent;          echo ' | ';    echo 'Children: ';      foreach ($children as $child) {      if (($wp_query->query_vars[category__in][$count]) != ($wp_query->query_vars[cat])) {       echo $wp_query->query_vars[category__in][$count];       echo ' ';      }     $count++;    }    

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »