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.

 

Comments

One Response to “A Blog Index for WordPress”

  1. [...] of them is my Bindex script which creates a blog index for WordPress. Why would I want to do that, you ask? Well, a client had lots of posts in many categories, and they [...]

Leave a Reply

feedbutton

Phone: 918-254-9730
Address:
125 S. Kalanchoe Ave.
Broken Arrow OK 74012

Follow Us on Twitter

Become a Fan -- Facebook

Latest News

WordPress Internal Permalinks by ID Shortcode

Tired of chasing down your internal links in WordPress? This shortcode is for you!

Solution: WordPress Upgrade and Image Upload Errors

If you’ve ever had an “incompatible archive” or “saving media attachment” error when working with WordPress, the answer may be simpler than you think.

Adding Additional Menus in WordPress 3.0

This multiple menu function really makes menu creation much easier. There wasn’t a lot of documentation, so I wrote some