New Hack for Displaying a Category / Subcategory Menu on WordPress

Visit http://www.wpworking.com/hacks-2/new-hack-for-displaying-a-category-subcategory-menu-on-wordpress/

This post is very similar to the last one, but now we will show also subcategories onthe menu.

Again, you just have to copy and paste the code bellow wherever you want on your theme files.

You can also play with its CSS styles, so using the ids divcateg, ulcateg and the dynamic generated licateg_[category slug], ulsubcateg_[subcategory slug] and licateg_[subcategory slug]. You may change the category / subcategories menu appearance.

<?
$args = array(
'type'                     => 'posts',
'orderby'                  => 'name',
'order'                    => 'ASC',
'hide_empty'               => 1,
'hierarchical'             => 1,
'taxonomy'                 => 'category',
'pad_counts'               => false );

$categories = get_categories($args);
if(count($categories)>0):
	echo '<div id="divcateg"><ul id="ulcateg">';
	foreach ($categories as $category) {
		echo '<li id="licateg_'.$category->slug.'"><a href="'.get_bloginfo('siteurl')."/".$category->slug.'">'.$category->cat_name.'</a>';
		$subcategories=  get_categories('child_of='.intval($category->cat_ID));
				foreach ($subcategories as $subcategory) {
					echo '<ul id="ulsubcateg_'.$subcategory->slug.'">';
					echo '<li id="licateg_'.$subcategory->slug.'"><a href="'.get_bloginfo('siteurl')."/".$category->slug."/".$subcategory->slug.'">'.$subcategory->cat_name.'</a></li></ul>';
				}
		echo '</li>';
	}
	echo '</ul></div>';
endif;
?>

,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: