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?
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 = ?
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";
}
?>
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++;
}
加载中,请稍侯......
精彩评论