Posts Tagged hacks

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;
?>
Advertisements

,

Leave a comment

New Hack for Displaying a Category Menu on WordPress

Check also this same hack, displaying also subcategories.

Hi there,

If you want to display a menu containing links for every post category page on your WordPress site, probably this hack will be useful.

Just copy and paste 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], you may change the category 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></li>';
	}
	echo '</ul></div>';
endif;
?>

,

1 Comment

%d bloggers like this: