Category tree
-
HI everybody.
While working on a small site I wrote a plugin to identify a category’s parent. Basically it was a is_category function but checked the parent instead of the actual cat.
Worked fine for me because i had to check only 1 level of nesting but i realized that it woudn’t work otherwise.
So i tried to write something more useful and i came out with the code below.
It checks if a category belong to a certain lineage.
I did a little testing and it looks ike it’s working but i’m no php expert. For this same reason i’m not planning to release this plugin.
I’m posting it here though so if somebody wants to grab it and maybe test it a little more here it is.
I’m curious to see if finally managed to give something back to this community! ??<?
/*
Plugin Name: Is category tree
Plugin URI:
Description: It checks if a category belong to a certain lineage.
Version: 0.01
Author: Filippo Petrecca([email protected])
Author URI:*/
function grz_is_category_tree ($parent='') {
global $wpdb, $wp_query;if (empty($parent)) {
return true;
}
$cat_obj = $wp_query->get_queried_object();
$cat_parent = $cat_obj->category_parent;while ($cat_parent <> 0) {
if ($parent == $cat_parent)
return true;
else
$cat_parent = grz_get_parent ($cat_parent);
}
return false;
}function grz_get_parent ($node) {
global $wpdb;
$wpdb->hide_errors();
$call="SELECT category_parent FROM $wpdb->categories WHERE cat_ID = $node";
$dadcat=$wpdb->get_var($call);
$wpdb->show_errors();
return $dadcat;
}?>
I have to thank Torben Brams of tbr_article_family plugin. His plugin showed me the way.
- The topic ‘Category tree’ is closed to new replies.