PowenKo, WordPress Tutorial 30-2, category, get all categories hierchy

November 26, 2011   ·   0 Comments

Screen Shot 2011-11-25 at 7.30.08 PM

 

 

GET TOP PARENT CATEGORY (WORDPRESS),

There are many times when you need to show or get the top most (root) parent category in WordPress – regardless of how many subcategories you might be deep. I have used this logic for page navigation (highlight the top parent tab) – as well as within some custom loops/sidebar code.

They way to do this:

 

 

 

<?php

function get_cat_hierchy($parent,$args){

	$cats = get_categories($args);
	$ret = new stdClass;

	foreach($cats as $cat){
		if($cat->parent==$parent){
			$id = $cat->cat_ID;

			$ret->$id = $cat;
			$ret->$id->children = get_cat_hierchy($id,$args);
		}
	}

	return $ret;
}
$args = array(
		'type'                     => 'post',
		'child_of'                 => 0,
		'hide_empty'               => false,
		'hierarchical'             => true,
		'exclude'                  => '1'
	);
$retarray=get_cat_hierchy(0,$args);
print_r($retarray);
echo "<p><p><p><p>";
/?>

shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Wordpress99

Wordpress

Amazon

adsbygoogle

Recent Posts

Categories

Amazon