WordPress Loop in a Static Homepage
The new WordPress 2.1 static homepage feature is awesome and finally makes WordPress a more viable contender in the greater Content Management System (CMS) fight but it has its difficulties. One of the main tricks to learn is how to access the main WordPress “Loop” in a static homepage. In my previous post, WordPress 2.1 static homepage , I mentioned the need to go about this differently and after multiple requests I decided to post a quick write-up on just how to do this.
The Problem: When you copy over your old template the WordPress PHP call to access the Loop now accesses just the loop for that particular page and not the global Loop. What this means is instead of displaying all your posts or even a few it just displays whatever you entered in the content box for your Static Homepage when you created it, following my writeup directions of course.
The Standard Loop Code:
<?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
<div class="post">
<?php require('post.php'); ?>
<?php comments_template(); // Get wp-comments.php template ?>
</div>
<?php endforeach; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
The fix: Before the php code that uses the loop, you need to replace $posts with the global loop instead. To do that you call the WordPress get_posts(); function like so:
<?php $posts = get_posts( "numberposts=6" ); ?>
In the above code the variable “numberposts=6″ tells WordPress how many posts to retrieve and populate the loop with.
You can also specify a category if you want to only display posts from a certain category say …. Homepage?? First you go to the admin section under Categories and find the ID of the category you want to display and then change the get_posts(); call to be something like:
<?php $posts = get_posts( "category=10&numberposts=6" ); ?>
You can pass get_posts(); many other parameters so check out the WordPress codex for more information.
Formatting: Since the homepage is a one-off part of your site you probably do not want to include the post.php file as in the normal loop above. I have removed that and the comment include in my site and replaced them with a more homepage friendly version which is below. I am using the the_excerpt(); function which displays a couple hundred characters of your post body as well as the the_title(), the_permalink(), the_time(), etc functions to build my own post display on the homepage.
<?php $posts = get_posts( "category=10&numberposts=6" ); ?>
<?php if( $posts ) : ?>
<?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<div class="post">
<p class="post-info-home"><?php the_title(); ?></p>
<div class="post-title-home">
<em><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">READ ENTIRE POST</a></em><?php the_time('d M Y h:i a'); ?>
</div>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
Let me know if this helped you and if you still have more questions.
Technorati Tags: WordPress, Static Homepage, WordPress Loop, WordPress 2.1
10 Responses to “WordPress Loop in a Static Homepage”
April 25th, 2007 at 7:43 am
Hi Richard,
Thank you for this. I like the fact that it doesn’t require a plug-in, and it offers more options for managing the dynamic homepage content than what I’m using now.
Best,
Steve
May 24th, 2007 at 6:39 pm
Thank you very much for publishing this code. It made my life so much easier. With a slight modification (stating the location of wp-config.php, and css), I got exactly what i needed.
June 6th, 2007 at 6:10 pm
Hi Richard,
I’m not sure what my problem is, but I cannot, for the life of me, get your code to work when I know it should. I have been using WP for quite some time and would love to figure out what I am doing wrong, but even after following your directions step-by-step I can’t get it to work.
Any thoughts, advice or words of encouragement would be tremendously appreciated.
Great site by the way!
Thanks in advance,
Dave
July 1st, 2007 at 10:15 am
Thanks so much for this, it is exactly what I have been looking for!
July 27th, 2007 at 3:59 pm
Hi Again,
This code has been working out quite well for me. I wanted to know though, how I could alter it to exclude multiple categories. (As in if an entry is from my music or movies categories, it is skipped and the next most recent entry’s excerpt is displayed.)
Thanks,
-Beth
September 28th, 2007 at 12:44 am
i can’t get the example code to work on my static homepage what am i doing wrong?
now the tricky part is i’m including it in an entry meaning a blog entry but i am using runphp which seems to work before
i can’t get any loops working on my static homepage and have tried a bunch
February 7th, 2008 at 2:42 pm
I thought that you’d led me to the answer, but I’m not quite there. Maybe you could help.
I used to just make a file in the /wordpress directory that I called news.php which had [include wp-blog-header.php] line and then the loop. This no longer works.
Do you think there is an easy way to make this work again? (The reason is I used to just use that as a templateless file which I called with a php include from my homepage.)
December 25th, 2008 at 1:26 pm
This is exactly what I needed – thank you so much for posting this!
April 25th, 2007 at 5:27 am
This is exactly what I needed and does the trick! I only tried the first part, but I’m going to try the excerpt part when I get home from work today. Thanks so much, this is incredibly helpful!!!