Don’t Show Me the Same List Twice

Filed as Features, Guides on July 6, 2007 1:27 pm

If you’re like the thousands of other bloggers out there, when you first started your blog, you went to the theme viewer or did a google search for free WordPress themes and found yourself something that you liked. Personally, I’m not a fan of themes that display the seemingly endless list of full articles right there on the homepage. I’d much rather see a couple of feature stories, and a list of either older stories, or a list of categories that I can browse through. But alas, even the free themes I’ve released have had the endless, reverse-chronological list of stories with full text and a “previous – next” link at the bottom.

And this is the case for most of the free themes out there.

What’s even funnier is the fact that some bloggers/theme designers put a list in a sidebar called “recent stories/articles”.

What?! You mean that not only do I get the long list of stories on the homepage, but I’m also getting another list in the sidebar with the exact same list? Seems like a waste of real estate, huh?

But it doesn’t have to be. By implementing this simple trick, you can change the sidebar information to display only a list of stories that are older than the stories on the homepage. Here’s how…

The Prep

First thing to do is figure out how many stories are being displayed on your homepage. Go to the WordPress dashboard and click on Options – Writing and find the section that says “Blog Pages” and check to see the number in the box that says “Show at most X posts”.

The Code

Now, find the section in your sidebar.php file that displays the recent articles. The template tag will look something like this:

<?php get_posts(‘numberposts=10′); ?>

This will output a list of your most recent 10 posts. In order to change this list to the most recent posts NOT on the homepage, we will need to offset this list. Remember the number of stories set to display on your homepage? Let’s say that number is 15. All you need to do is add the “offset” qualifier inside the parenthesis to get the list offset. It should look like this now:

<?php get_posts(‘numberposts=10&offset=15′); ?>

But what about when we leave the homepage?

Good question. If we’re not on the homepage, there’s no reason to offset the list. In fact, if someone comes to your site from an incoming link or google search, you WANT them to be exposed to your most recent posts. So, what we’ll do is use the is_home() conditional tag to only offset when we’re on the homepage. Everybody has their own opinion how how to write php conditions, but here’s what I would do:

<?php
if (is_home()) {
get_posts(‘numberposts=10&offset=15′);
}
else {
get_posts(‘numberposts=10′);
}
?>

Now, every time you’re on the homepage, your list is offset, and if you’re on any other page, the list displays the latest stories from your blog, not offset.

Simple huh?

Tags:

This post was written by

You can visit the for a short bio, more posts, and other information about the author.


Submissions & Subscriptions

Submit the post to Reddit, StumbleUpon, Digg or Del.icio.us.

Did you like it? Then subscribe to our RSS feed!



  1. By GoingLikeSixty posted on July 6, 2007 at 1:54 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Don’t show me your .org tips without clarifying it early on.

    Reply

  2. By William Profet from OneJobTwoSalaries.com posted on July 6, 2007 at 9:04 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    It works fine. Thank you!

    Reply

  3. By Jan posted on July 7, 2007 at 9:45 am
    Want an avatar? Get a gravatar! • You can link to this comment

    As I implemented on my blog I had problems with it because of the apostrophes you use. They are not only different, but also seems to get in the way of it working. After having replaced them with plain ‘ it worked.

    The one I used make use of slightly other commands, but luckily this “home” or “not home” tip could be integrated when I changed the commands as well. Thanks for the tip!

    Reply

  4. By Nathan Rice posted on July 7, 2007 at 2:43 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Sorry Jan,
    WordPress likes to change the quotes to the curly ones when you publish. Like you did, just replace them with regular old quotes. Glad you figured it out.

    Nathan

    Reply

  5. Blog News Watch » Blog Archive » Weekly Roundup: Week Ending July 6, 2007July 7, 2007 at 7:32 pm
  6. By Lynn posted on July 24, 2007 at 1:28 am
    Want an avatar? Get a gravatar! • You can link to this comment

    I like this solution and the fact you account for the Home page condition. However, I am using widgets, so the get_posts code isn’t in my sidebar.php (actually obar.php in my theme which has two sidebars). get_posts isn’t in the widgets.php file in the plugins folder either. I’m thinking I should revert to hand-coding the sidebars instead of using widgets to accomplish what I want. . . Or is there something else I can do?
    Many thanks for your solution, and any help in applying it when the widgets plugin is being used.

    Reply

  7. By Anthony Bradley posted on October 17, 2007 at 1:14 am
    Want an avatar? Get a gravatar! • You can link to this comment

    I tried the code but it didn’t work. It sometimes needs a variable assigning to get_posts so this is what worked for me.

    “>

    NOTE:

    I used $gposts as a variable instead of $posts for the following reason given by the WordPress Wiki for get_posts.

    Quote:

    Assigning get_posts() to the variable $posts, when you may already have it in use for The Loop, can lead to conflicts. If so, create a variable of your own (i.e. $gposts, $my_posts).

    Reply

    Your words are your own, so be nice and helpful if you can. If this is the first time you're posting a comment, it might go into moderation. Don't worry, it's not lost, so there's no need to repost it! We accept clean XHTML in comments, but don't overdo it please.

    Current day month ye@r *