Tomer Lichtash
Tomer Lichtash

Reputation: 9262

Check if_parent and if_child in Wordpress

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?

Upvotes: 0

Views: 1137

Answers (3)

Tomer Lichtash
Tomer Lichtash

Reputation: 9262

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++;
  }

Upvotes: 1

Adam Dempsey
Adam Dempsey

Reputation: 2962

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";
}
?>

Upvotes: 2

djc
djc

Reputation: 11711

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 = ?

Upvotes: 2

Related Questions