Custom Category Templates on a Archive or Index page.

On my home page and in my archives, I use a custom category template to display asides and news articles. This is very easy to do and it only takes a couple seconds of work to create custom category templates in any WordPress theme.

The first step is to add the following to your current theme’s index.php loop after the line that looks like < ?php while (have_posts()) : the_post(); ?>, but before any other code.

< ?php $cat_temp = cat_loop();?>
	< ?php if($cat_temp && is_numeric($cat_temp)){?>
		< ?php include('loops/cat_'.$cat_temp.'.php');?>
	< ?php }else{ ?>

Then add } just before the line endwhile.

The next step is to add the following to your theme’s functions.php file (you may have to create a file with the same name):

function cat_loop(){
	global $blog_id,$post, $wp_version;
		if($wp_version >= 2.3){
			global $object_term_cache;
			$array = $object_term_cache[$blog_id][$post->ID]['category'];
		}else{
			global $category_cache;
			$array = $category_cache[$blog_id][$post->ID];
		}
		while (list($cat) = each($array)) {
			if(file_exists(dirname(__FILE__).'/loops/cat_'.$cat.'.php')){
				return $cat;
			}
		}
	}

This can be modified to look at author’s also.

Continue reading. »

Displaying WordPress categories in a horizontal dropdown menu.

One of my readers recently asked how I created my horizontal menu bar: the short answer is by mixing CSS and Javascript.

The first step is to get WordPress to display the menu as a hierarchical list without a title. < ?php wp_list_categories('sort_column=name&sort_order=asc&style=list&children=true&hierarchical=true&title_li=0'); ?>

We then wrap this WordPress code in the following so we can style it.

<div style="text-align:center;">
    <ul id="menu" style="padding:0; margin:0;">
        < ?php wp_list_categories('sort_column=name&sort_order=asc&style=list&children=true&hierarchical=true&title_li=0'); ?>
    </ul>
</div>

I added this to my header.php, but you can add it anywhere you want it to appear.

The CSS is fairly simple and you just need to add it to your theme’s style.css file.

ul#menu {
	margin: 0;
	padding: 0;
	list-style: none;
	width: 100%;
	font-size:1.2em;
}

ul#menu li {
	float: left;
	padding: 0;
	margin: 0;
	border-right:solid 1px #fff;
}

ul#menu ul li {
	float: none;
	position: relative;
	border-bottom: 1px solid #7EAED7; /* fixes gap problem in IE */
	border-left: 1px solid #FFF;
	z-index:1000;
}

ul#menu li ul {
	margin: 0;
	padding: 0;
	display:none;
	list-style: none;
	position: absolute;
	background: #9CC;
}
ul#menu ul ul{
	margin-left: .2em;
	position: absolute;
	top: 0; /* if using borders, -1px to align top borders */
	left: 100%;
}

ul#menu * a:hover, ul#menu li a:active{
background:#7EAED7 !important;
color: #FFFFFF;
}

ul#menu li a:link,
ul#menu li a:visited,
ul#menu li a:hover,
ul#menu  li a:active{
	display: block;
	padding: .2em .3em;
	text-decoration: none;
	background: #5587B3;
	 color: #FFFFFF;
}


ul#menu ul li a:link,
ul#menu ul li a:visited,
ul#menu ul li a:hover,
ul#menu ul li a:active {
	width: 8em;
}

Of course you will need to change the colors and text sizes to ensure it blends with the rest of the theme.

Continue reading. »