So I wanted my most recent entry to be featured and at the same time have a short asides section. Each on their own isn’t tough to implement but getting those asides not to show up as a featured post took a little trickery.
Initially my featured post was a loop in of itself. From the WordPress Codex, I got the following code as an example of using multiple loops:
have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID;?> ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
The first line initiates a new query. Since I wanted it to be the most recent post and not just posts from a certain category, I removed the category_name=featured. You can read more about the rest of the stuff over at the Codex, but basically it pulls out the first post and allows you to do whatever it is you want to it. Then the rest of the posts could be displayed separately.
Next up were the asides. Again, I went to the Codex (what a wonderful source of info, albeit a little poorly organized) and found the following for asides:
Also, since that came after my main loop for content, I had to add
That ‘rewinds’ the loop so we can go through it again, looking for posts in the specified category. Of course, the category 14 in this case is arbitrary; I changed that to the ID corresponding to my asides category. The problem is, the featured and main content loops would still find these posts and display them, so we want to prevent these from being displayed. Normally, this would be quite simple, just throw in a ‘if not in category(x)’ into the main loop. However with the featured post implemented as it is above, there’s no way to prevent an aside from being displayed as the featured post, if that aside is the newest post. WP_Query (above) with the argument showposts=1 will return the most recent post. As far as I know, there’s no way to exclude a category.
After some thinking, I decided to rewrite the featured post implementation. Since querying the latest post wasn’t working well with the asides, I implemented a simple counter within a single loop for both the featured post and the normal content. That loop also had a conditional statement, excluding my asides category. Then, if the loop counter was equal to 1, I’d style the post a certain way as the featured, and otherwise, it would be styled as normal content. Use the following code as a framework.
The disadvantage of that implementation is while posts in the specified category are not displayed in the loop, any encountered will still be counted as being displayed. As a result, whatever number of posts you’ve set to show in the Admin options may be reduced by the number of asides which were encountered while looping through the most recent ‘x’ posts. I’m still working on a way around this and would appreciate any help.