Occasionally you might find the need to customize a category page for a specific category or post archive. Perhaps you want to show some additional text, images or include some other content relevant to that category.

I had a client who wanted me to customize a category page to display a random image starting from a random post in the particular category along with next/prev buttons for navigation through the posts. The client wanted to link directly to this custom category page from a menu item so instead of linking to category.php I created a copy of the themes category.php and renamed it category-. I got the category number (cat_num) from Posts, Categories (hover over the category and in the status bar you’ll see the cat_id). So, if the category number is 234 then the page is named category-234.php.

For this application first I had to randomize the posts:

$rand_posts = query_posts($query_string . '&numberposts=1&orderby=rand&cat=3649&posts_per_page=1');

Then I started the Loop and I got a random image for each post.

$random_image=&get_children( 'post_type=attachment&post_mime_type=image&numberposts=1&orderby=rand&post_parent='.$post->ID );

As an image is classed as an attachment I used the wp_get_attachment_image function to pick up an image and echo it to the browser.

foreach ( $random_image as $attachment_id => $attachment ) {
	echo wp_get_attachment_image( $attachment_id, 'full' );

With some customized Next/Prev buttons this category page for my client turned into an easy random funny image gallery. I linked to the page directly from the main menu.