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. Now the only thing you have to do is to create a folder named “loops” in your theme’s folder, and then create a new file with a new “loop” — excluding the while and endwhile parts — and name it cat_xxx.php where xxx is the id of the category the loops is for.

This can be repeated for single.php, archive.php, or anywhere else a custom loop is useful. You can even use something similar for individual posts or pages.

A post may be in multiple categories, but it will only use the first custom template it finds, so make sure the post is in only one specially styled category at a time.

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.

Now the last step is the to make it work as a drop-down list on all browsers. This could be done in Firefox with a simple CSS declaration, but Internet Explorer doesn’t understand the :hover pseudo classes, so we mimic this in Javascript.

<script type="text/javascript">
 /*<![CDATA[*/

var mbA,mbT,mbTf,mbSf;
var mbR = [];

function mbSet(m) {
if (document.getElementById&&document.createElement) {
	var m=document.getElementById(m);
	mbR[mbR.length] = m;
	var i;

	e=m.getElementsByTagName('a');
	if (!mbTf) mbTf=new Function('mbHT();');
	if (!mbSf) mbSf=new Function('mbS(this);');
	for (i=0;i<e.length;i++) {
		e[i].onmouseout=e[i].onblur=mbTf;
		e[i].onmouseover=e[i].onfocus=mbSf;
	}

	m=m.getElementsByTagName('ul');
	for (i=0;i<m.length;i++) {
		mbH(mbL(m[i]));
	}
}}

function mbHA() {
	if (mbA) {
		while (mbA) mbH(mbA);
		mbHE('block');
	}
}

function mbHT() {
	if (!mbT) mbT=setTimeout('mbHA();', 0);
}

function mbTC() {
	if (mbT) {
		clearTimeout(mbT);
		mbT=null;
	}
}

function mbS(m) {
	mbTC();
	if (mbA) while (mbA&&m!=mbA&&mbP(m)!=mbA) mbH(mbA);
	else mbHE('none');

	if (mbM(m)) {
		mbSH(m,'block');
		mbA=m;
	}
}

function mbH(m) {
	if (m==mbA) mbA=mbP(m);
	mbSH(m,'none');
	mbT=null;
}

function mbL(m) {
	while (m && m.tagName != 'A') m = m.previousSibling;
	return m;
}

function mbM(l) {
	while (l && l.tagName != 'UL') l = l.nextSibling;
	return l;
}

function mbP(m) {
	var p = m.parentNode.parentNode;
	if (p.tagName == 'UL') {
		var i = 0;
		while (i < mbR.length) {
			if (mbR[i] == p) return null;
			i++;
		}
	} else {
		return null;
	}
	return mbL(p);
}

function mbSH(m,v) {
	m.className=v;
	mbM(m).style.display=v;
}

function mbHE(v) {
	mbHEV(v,document.getElementsByTagName('select'));
}

function mbHEV(v,e) {
	for (var i=0;i<e.length;i++) e[i].style.display=v;
}
/*]]>*/
</script>

A couple notes on the previous code.

  1. To activate it, change your theme’s <body> tag to <body onload=”mbSet(‘menu’);>
  2. It was not written by me, but I’ve been using it for a long time and don’t remember where I got it.
  3. It can occasionally create a JavaScript error, and I’ve intended to rewrite it for a long time, but it usually works fine.

Designing flexible WordPress themes.

The average WordPress theme has different files for pages, single posts, archives and the front page; however, most of them are almost exactly the same except for inside the_loop. This is a quick tutorial on how to do the most with the fewest files, and includes a few methods to have custom templates by separating content display from structural elements.

WordPress looks first for special files and then defaults to the index.php file (as shown in this diagram). We can take advantage of this by using only an index.php file and then using conditionals to modify it. While it seems that this method would render the code less readable, it is actually far more readable, and far easier to modify.

Your theme should start off a header.php, footer.php, sidebar.php and an index.php. These files are the “code” files and are fairly self-explanatory, and at this point the only question should be whether you add just the header portion of the file to the header.php or everything including the calls to get_sidebar()—the same goes for the footer.php depending on your theme. This depends on how you use your them, if you plan to integrate it with bbPress and use the same header and sidebars, you should add everything before the normal loop to the header. However, we will focus on things outside of the header (this includes the navigation menus and such) and just focus on the_loop.

A “normal” index page looks a little like this. (Taken from the default WordPress theme.)

<?php get_header(); ?>

	<div id="content" class="narrowcolumn">

	<?php if (have_posts()) : ?>

		<?php while (have_posts()) : the_post(); ?>

			<div class="post" id="post-<?php the_ID(); ?>">
				<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
				<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>

				<div class="entry">
					<?php the_content('Read the rest of this entry &raquo;'); ?>
				</div>

				<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
			</div>

		<?php endwhile; ?>

		<div class="navigation">
			<div class="alignleft"><?php next_posts_link('&laquo; Previous Entries') ?></div>
			<div class="alignright"><?php previous_posts_link('Next Entries &raquo;') ?></div>
		</div>

	<?php else : ?>

		<h2 class="center">Not Found</h2>
		<p class="center">Sorry, but you are looking for something that isn't here.</p>
		<?php include (TEMPLATEPATH . "/searchform.php"); ?>

	<?php endif; ?>

	</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

For most themes, the page and single pages look the same: the only part that really changes is the_loop itself, and even then the changes are usually minor, so if you want to change something outside the_loop, you have to change it in all files. However, by operating the majority of display elements out of the main files, we can make it infinitely extensible without overloading the end-user with large amounts of code to sort through.

The index.php file for the theme I use is very short and easily read.

<?php get_header(); ?>
	<div id="wrap">

		<div id="content">

			<?php if (have_posts()) : ?>

				<?php
					if(is_home()){

						include (TEMPLATEPATH . '/index-loop.php');

					}elseif(is_page() || is_single()){

						include (TEMPLATEPATH . '/single-loop.php');

					}else{

						include (TEMPLATEPATH . '/archive-loop.php');

					}
				?>

			<?php else :?>
				<h2 class="center">Not Found</h2>
				<?php include (TEMPLATEPATH . '/not-found.php');?>
			<?php endif; ?>

		</div>

		<?php get_sidebar(); ?>

	</div>

<?php get_footer(); ?>

If we break it down: it calls the header, adds the two structural divs, and checks to see if there are any posts (of any kind). If there are no posts it includes an error page before including the sidebar, closing the divs and calling the footer. This part of the page is fairly normal and is used in most themes—not only is it used in most themes, but most of the main files all have this same code–every single theme file calls the footer, sidebar and header, shows an error message if there are no posts to display. This is a lot of wasted space. If you suddenly want to change the name of something or move the sidebars, you have to change every file which is just pointless. The difference in my index.php file is that there isn’t a full loop. It checks if there are posts using have_posts(), but where you would normally see a loop it just has some conditional statements, and for each condition, a different loop is included.

Each of the loops is in a different included file. (The loops are shown below, but just notice the similarities and major differences.)

Index loop:

<?php while (have_posts()) : the_post(); ?>
		<h2 id="post-<?php the_ID(); ?>" class="b">
			<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?> </a>
		</h2>

		<div class="main main_border">

			<small>
				Posted on <?php /*backslashes escape chars*/ the_time('l \t\h\e jS \o\f F, Y \a\t g:i a ') ?> in <?php the_category(', ') ?> <?php edit_post_link('Edit','-');?>
			</small>

				<?php the_content('Continue reading "'.the_title('', '', false).'"')?>

			<div class="small box">
				//Some comment and UTW stuff				
			</div>	
		</div>
<?php endwhile; ?>

<div class="main" style="width:99%;">
	<div class="main-nav-left" style="float:left;width:49%;"><?php posts_nav_link('','','&laquo; Previous Entries') ?></div>
	<div class="main-nav-right" style="float:right;width:49%; text-align:right;"><?php posts_nav_link('','Next Entries &raquo;','') ?></div>
</div>

single-loop.php (handles both singles and pages because there is only a tiny difference between the two)

<?php while (have_posts()) : the_post(); ?>

	<h2><?php the_title();?></h2>


	<small>
		<?php if(!is_page()){ /*Only difference between pages and singles*/?>

			Posted on <?php /*backslashes escape chars*/ the_time('l \t\h\e jS \o\f F, Y \a\t g:i a ') ?> <!-- by <?php the_author() ?> -->in <?php the_category(', ') ?> <?php edit_post_link('Edit','-');?>

		<?php }else{?>

			<?php edit_post_link('Edit','-');?>

		<?php } ?>
	</small>

	<div class="main">

		<?php the_content('Read the rest of this entry &raquo;'); ?>

		<?php wp_link_pages(); ?>

		<div class="small box">

			//some comment and UTW stuff

		</div>

		<?php posts_nav_link(' &#8212; ', __('&laquo; Previous Page'), __('Next Page &raquo;')); ?>

	</div>

<?php endwhile; ?>

And the archive-loop.php which handles categories, date archives, tags, searches etc.

<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>

	<?php /* If this is a category archive */ if (is_category()) { ?>

		<h2> Reading about <?php single_cat_title(); ?></h2>

 	<?php /* If this is a daily archive */ } elseif (is_day()) { ?>

		<h2>Archive for <?php the_time('F jS, Y'); ?></h2>

	<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>

		<h2>Archive for <?php the_time('F, Y'); ?></h2>

	<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>

		<h2>Archive for <?php the_time('Y'); ?></h2>

	<?php /* If this is a search */ } elseif (is_search()) { ?>

		<h2>The Search Results For "<?php echo $s;?>"</h2>

	<?php /* If this is an author archive */ } elseif (is_author()) { ?>

		<h2>Author Archive</h2>

	<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>

		<h2>Archives</h2>

	<?php } ?>


	<div class="main" style="width:99%;">
		<div class="main-nav-left" style="float:left;width:40%;"><?php previous_posts_link('&laquo; Previous Entries') ?></div>
		<div class="main-nav-right" style="float:right;width:40%; text-align:right;"><?php next_posts_link('Next Entries &raquo;') ?></div>
	</div>


	<?php while (have_posts()) : the_post(); ?>

	<h3 class="b"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link a <?php the_title(); ?>"><?php the_title(); ?></a></h3>

	<div class="main main_border <?php echo anth::styles();?>">

		<small>
			<!-- Posted <?php the_time('j F Y') ?>  by <?php the_author() ?> -->
			<?php if(function_exists('UTW_ShowTagsForCurrentPost')){?>
				<span class="utwtags">Keywords: <?php UTW_ShowTagsForCurrentPost("commalist") ?></span>
			<?php } ?>
			<?php edit_post_link('Edit','-');?>
		</small>

			<?php the_excerpt() ?>

	</div>

<?php endwhile; ?>

<br/>

	<div class="main" style="width:99%;">
		<div class="main-nav-left" style="float:left;width:40%;"><?php previous_posts_link('&laquo; Previous Entries') ?></div>
		<div class="main-nav-right" style="float:right;width:40%; text-align:right;"><?php next_posts_link('Next Entries &raquo;') ?></div>
	</div>

The actual loops in each of these files starts with <?php while (have_posts()) : the_post(); ?>, and you will notice that all the loops are similar with only a few cosmetic changes, the major difference is in the archive page which uses a large conditional block to set the title text, but this could have been done on the index.php page and used a completely different loop if you needed it.

Okay, so you get down to here and wonder why you haven’t heard anything new. well once we start using custom loops the possibilities are endless.

For example, as I posted about recently, on my index I use a special loop for items that are in the Asides category, but only on the index page, so inside of my is_home) conditional statement I also add the code:

<?php $in_cat = cat_loop();?>
	<?php if($in_cat){?>
		<?php include('cat_'.$in_cat.'.php');?>
	<?php } ?>

Which calls the function: (added to the functions.php file)

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__).'/cat_'.$cat.'.php')){
				return $cat;
			}
		}
	}

Basically the function loops through all the categories a post is in and if there is a custom loop for a category it uses it. This can easily be extended for author ids, name or anything else that you can test.

Let’s review: use as little repeated code as possible because when you do lots of tricks are available to you.

Creating a World- An Introduction and The Goals

I have always wanted to write a book. Not just any book; a good one. One that creates an entire universe and populates it; maybe not even a single book, maybe a whole series. Anyway, that’s not why this whole section is here. Instead this section will create a universe. As a basic foundation it will be similar to any Fantasy book or movie you have read. There will be Elves, Dwarfs, Goblins and Dragons. Instead of creating a world defined by the genre and within a tired cliche, I will try to breath new life into the old themes; in such a way that the universe I create is unique while still be familiar and accessible. Continue reading »