Wordpress: Featured post and one post from each category
Written By mukundlakshman on Jun. 21, 2007.
7 Comments
Report Note
+ Clip This
I have a bit of code, but it seems horrendous. I have to include 7 loops - eight categories and a 'featured' post with some special CSS styling.
Can I simplify this? Can I pull the posts out of the SQL database directly, to avoid all the loops, and will that speed up page load times? Is there a plugin that already does some/most of this? Should I be using another CMS? My layout is as follows:
So, to clarify, each of the 'Category' sections will have only the most recent post from that category. The relevant snip of code:
<?php
while (have_posts()) : the_post();
$feature = get_the_category(); $feature = $feature[0]->cat_ID; ?> <!-- $feature is the ID of the category of the feature post, used later to ensure that the feature post isn't repeated. -->
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php
$arr = array(1, 2); // Grouped into sets of two categories.
foreach ($arr as $catno) {
if ($feature == $catno) { $offset = 1; } else { $offset = 0; } <!-- If feature is in selected category, offset by one so the feature post isn't repeated. -->
query_posts('cat=' . $catno. '&showposts=1&offset=' .$offset);
while (have_posts()) : the_post();
?>
<?php the_excerpt(); ?>
<?php endwhile; } ?>
And the full code. I'm not afraid to play with PHP, but I don't know Wordpress or its hooks (aside from the basic template tags stuff).
Thank you guys, this is my first note!

StevenCampbell
Written Jun. 21, 2007 / Report /
I've tried pulling posts directly out of the database - it certainly works, but is very complicated. Probably more complicated than using seven loops.
blogosquare
Written Jun. 21, 2007 / Report /
My opinion in all this code stuff would be to use the customisable post listing plugins
I've been using this to pull post from a specific category called my famous post, in your case the featured category.
As for pulling the post loops, check out Derek's theme, I think it's the October Special or some other of his theme that has the code. Do download them and study how things are shown.
SasaVtec
Written Jun. 21, 2007 / Report /
Thanks for the code, will have to try this out if I can get it to work :(.
ryanarrowsmith
Written Jun. 21, 2007 / Report /
Actually, this is much more simple than you'd think. It can be accomplished without a plugin by querying the database directly. I've put up the source code here.
Basically, all you need to do is replace your normal loop with this query. It'll pull the number of articles from the category you specify. Use the first one for your Featured Post and style it, then use it again for more recent entries and style it differently. Let me know if you have trouble.
mukundlakshman
Written Jun. 21, 2007 / Report /
@blogosquare: I like Scott's plugins, but it's limited in terms of formatting my code. In my design, each category section will have a corresponding image, output using a
<?php post_image(); ?>tag. Since Scott doesn't actually invoke the loop, the post_image won't work.I think I'm making this unnecessarily complicated. Here's an image of what I'm trying to do:
LayoutImage.
I'm trying to do something very similar to what Derek does in his blog [thanks for the link] with the categories, so I'll email him. It's not in any of his public themes, though.
I'm just worried that running 7 loops (could become 9 loops if we decide to add 2 sections to the paper next year) could be PHP overload. Ideally, I'd like to do something like:
FOREACH $category SELECT most_recent_post -> $arrayOfPosts;(etc.)echo $arrayOfPosts[0];
echo $arrayOfPosts[1];
ryanarrowsmith
Written Jun. 21, 2007 / Report /
Muk: What you're trying to do can be accomplished by querying the category directly since you want info from a specific category. You don't need to run the full loop at all. Just post the code I put above and update the category name, quantity of articles and then the formatting.
I don't know for sure, but I'd think this is pretty similar to how Derek executes his homepage.
mukundlakshman
Written Jun. 23, 2007 / Report /
Thank you guys. I'm currently using a lot of PHP, but I think I can optimize it a bit by using a single loop with a couple switch-case's.
But here's the semi-finished, but has the general idea page:
Eastside v2
Warning: Not tested in non-Firefox browsers, will not be tested until I get my copy of Parallels running :]