How To Create A Page That Automagically Updates Itself - Smashing WordPress Tutorials #2

Yo there! If you are new here, you may want to subscribe to my RSS feed, or through email, for more tips on better and effective blogging!

In the previous edition of Smashing WordPress tutorials, I told you how toeasily make WordPress archives that rock. You might want to check that out :)

Today, I’m going to show you a very useful little technique you can use to automatically update pages to certain content. I’ll explain:

I have a links roundup every week, on Thursdays. I created a separate page for these roundups, which contains links to all the roundups I’ve ever done. And when I publish a new roundup, that page automatically updates itself to include the new link. So, I don’t need to worry about manually updating the page ever again.

Useful Uses

There are several useful uses of this technique. For one, like me, you can create such a dedicated page for, say your weekly top ten lists, and have it automatically updated each time you publish a post.

Another use for this could be to group all important things, such as press releases, together, to organize them without you having to sort out through the other posts.

What We Will Do

OK let’s start with this tutorial. What we’ll do is that we will assign each of the required posts a dedicated category, and then use a WordPress template tag to call all the posts from that category onto our dedicated page. Let’s start!

Step 1: Assign a Category

If you haven’t already, give each of the required posts a category. You need to know the category ’slug’. You can find that in your Manage Categories page, on your category page:

Note that category slug

Step 2: Create a Dedicated Page

This step is a technical one. What you need to do is copy your page.php file, and rename it to something else (like links.php, for me). Delete the <?php the_content(); ?> line.

Add this in place of that:

<ul>
<?php
$posts = get_posts(’numberposts=50&category_name="links-roundup"&orderby=post_title&order=DESC’);
foreach($posts as $post):?>

<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>

<?php endforeach; ?>
</ul>

Now for the explanation. What happens here is that first you define an unordered list (<ul>) to hold all the list items (<li>) generated. The get_posts template tag gets the posts you want. The numberposts attribute is set to -1, to show all posts. The ‘-1′ removes the limit on the number of posts to be shown.

The category_name attribute is set to links-roundup, which you should change to your own category’s name. Don’t forget the double quotes!

The orderby attribute is set to post_title, to order all posts by their titles. And finally, the order attribute is set to DESC to show the latest post first.

Step 3: Make The Page A Template

Now, you need to make this page of yours a template, so you can create a new page in your WordPress blog based on this one. Insert these lines at the very top of your page:

<?php
/*
Template Name: Roundups
*/
?>

You can name the template pretty much anything you want, it doesn’t really matter. Now, save this page, and upload it to your server, if you haven’t done so already. Take care to upload it to your theme directory.

Step 4: Create a New Page in WordPress

We’re almost done!

Now, you need to create a new page in your WordPress blog, that’s based on the template we created. Go to the Write Page option, give your page a relevant title and slug. But, the cake is at the bottom: the Page Template section. Choose the name of the template you just created:

Then, publish the page! And you’re done!

Other Tips

You can add some flavour to your page by adding a description and other things. Simple write those things out around the code you just put in your dedicated page.

Also, the get_posts template tag has lots more potential. Learn more about how you can use it at the WordPress codex

Your Thoughts?

Have you ever felt the need for such a system? Do share your thoughts with the rest of us!


If you liked this post, consider subscribing to the RSS feed (what's RSS?) to get updates on new blog posts.
You can get our latest articles on blog strategy, design, WordPress and the like delivered to your inbox, free of charge. Just enter your email below:

3 Responses

  1. 1

    I have been searching for such a post for a long time now!Now, that i have got what i wanted, i can’t wait to try!

    August 28th, 2008 at 10:22 pm
  2. 2

    I am having a problem with the line that says:

    $posts = get_posts(’numberposts=50&category_name=”links-roundup”&orderby=post_title&order=DESC’);

    there is an aparent unexpected “=” syntax error.

    Any ideas?

    October 24th, 2008 at 6:21 pm

Trackbacks

  1. Leonaut.com

Leave a Reply?