A Blog Index for WordPress

Why didn’t I think of it sooner?

Do you use WordPress as a CMS and have a lot of blog posts in different categories? This script could be for you. Requires Template Modification

How it Works

Every install of WordPress has one page that displays the most recent posts. By default, its the front page and it uses the Main Index Template (index.php) which you can find under Appearance => Editor in the admin panel.

In the default theme, other themes may vary, there’s a section of code that starts with this:

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

and ends like this:

<?php get_search_form(); ?>
<?php endif; ?>

It’s a big wad of code that is most of the template.

Replace it with this:

<?php $categories= get_categories(); foreach ($categories as $cat) : ?>
<?php $this_one = $cat->category_nicename; ?>
<?php $this_cat = $cat->cat_name; ?>
<?php $category_id = get_cat_ID($this_cat); ?>
<?php $category_link = get_category_link( $category_id ); ?>
<div class="bindex_cat">
<h3 class="bindex_cat_header"><a href="<?php echo $category_link; ?>" title="<?php echo $this_cat ?>">
<?php echo $this_cat ?></a></h3>
<?php query_posts('category_name='.$this_one.'&showposts=1'); while (have_posts()): the_post(); ?>
<div class="bindex_item">
<h4 class="bindex_item_header"><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h4>
<div class="bindex_excerpt"><?php the_excerpt(); ?></div><!-- end excerpt -->
<div class="bindex_button"><a href="<?php echo $category_link; ?>" title="<?php echo $this_cat ?>">
More from this category</a></div><!-- end button -->
</div><!-- end item -->
<div class='clear'> </div>
</div><!-- end cat -->
<?php endwhile; endforeach;?>

Copy and paste the entire page code to a text file first, just in case…

Save your changes and take a look at your blog page

The Result

Now your blog page will show not just excerpts from your most recent posts, but the most recent posts in each category, with a link directing users to that category! Classes are added to the tags so you can style it to match your theme colors.

My news page is an example!

Now when people visit your blog page, they won’t see just your most recent posts, but the most recent post from each category.

View Comments

Leave a Reply