Return to Snippet

Revision: 14177
at September 23, 2009 09:11 by kristarella


Updated Code
function custom_archives_template() {
$cats = get_categories("hierarchical=0");
if ($cats) :
	echo '<h2>By Category:</h2>';
	foreach ($cats as $cat) :
		$cat_name = $cat->cat_name;
		$cat_id = $cat->cat_ID;
		$cat_link = get_category_link($cat_id);

		echo '<ul>';
			echo '	<li class="cat_name"><h3>' . $cat_name . ' <span class="count">' . $cat->category_count . '</span></h3>';
			$catposts = new WP_Query("showposts=-1&cat=$cat_id");
			if ($catposts) :
				echo '		<ul>';
			while ($catposts->have_posts()) : $catposts->the_post();
				echo '			<li><a href="' . get_permalink() . '">' . get_the_title() . '</a> <span>' . get_the_time('j F Y') . '</span></li>';
			endwhile;
				echo '		</ul>';
			endif;
			echo '	</li>';
		echo '</ul>';

	endforeach;
endif;
}
remove_action('thesis_hook_archives_template', 'thesis_archives_template');
add_action('thesis_hook_archives_template', 'custom_archives_template');

Revision: 14176
at May 21, 2009 23:55 by kristarella


Initial Code
function custom_archives_template() {
$cats = get_categories("hierarchical=0");
if($cats != NULL) { ?>

<h2><?php _e('By Category:', 'thesis'); ?></h2>
	<ul>
	<?php foreach ($cats as $cat) { ?>
		<li>

		<?php if($cat != NULL) {
			$base_url = get_bloginfo('home') . "/category/" . $cat->slug; ?>
			<h3><a href="<?php echo $base_url?>"><?php echo $cat->cat_name?></a></h3>
		<?php } ?>

		<?php // Show category description
			if ($cat->category_description != NULL) ?>
				<p><?php echo $cat->category_description ?></p>

				<?php $myposts = get_posts("category=$cat->cat_ID"); ?>
				<ul>
				<?php foreach($myposts as $post) : 
					$date = $post->post_date;
					$time = strtotime($date);
				?>
					<li><?php echo date("M jS, Y", $time); ?> <a href="<?php echo $post->guid; ?>"><?php echo $post->post_title; ?></a></li>
				<?php endforeach; ?>
				</ul>
			</li>
	<?php } ?>
	</ul>
<?php }
}
remove_action('thesis_hook_archives_template', 'thesis_archives_template');
add_action('thesis_hook_archives_template', 'custom_archives_template');

Initial URL
http://www.z-oc.com/blog/2008/03/category-based-archive/

Initial Description
This used to be code adapted from Z'Oc, but I decided to start again and write it fresh with WP_Query instead of get_posts.

Initial Title
Archives listing posts by category

Initial Tags
wordpress

Initial Language
PHP