Creating Custom Blog Pages in WordPress

Posted on April 24th, 2010 in wordpress

I’m posting this here mostly so I can remember how I did it. If you happen to find this useful yourself, then great :)

One of the problems I commonly have with WordPress is customizing archive-type pages. I usually want to put some piece of content above the archives — and need that content to be editable by the client, so it can’t be hard-coded. And I don’t want just a list of archives, I want titles, meta info, excepts. Yeah, I want a lot of things.

After much hair-pulling (cuz my PHP skillz are basic, at best), I came up with this solution that I used on my best friend’s wedding site.

1. Create a new template

Create a new file, call it say blog.php, and put the following at the top:

<?php
/**
Template Name: Blog
 */
get_header(); ?>

2. Add in the page’s content

Within the Loop, put the following:

 <h1><?php the_title(); ?></h1>
<div class="entry">
        <?php the_content(); ?>
</div>

3. Add in the list of archives

After the page’s content, and before the end of the Loop, stick this in there:

 <?php
 $postslist = get_posts('numberposts=-1&orderby=date');
 foreach ($postslist as $post) : 
    setup_postdata($post);
 ?>
      <div>
        <h2 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
        <p class="postmetadata">By <?php the_author(); ?> on <?php the_time('l, F jS, Y') ?> in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></p>
        <div class="entry">
          <?php the_excerpt() ?>
        </div>
      </div>
<?php endforeach; ?>

(This is the list of parameters that get_posts() can have).

4. Create the Page

Now go into your WP admin, create a new page, and select Blog from the template list.

And that’s it. We’re done. The end.

EAVB_DTRPWQAUFX

Leave a Reply