How the #@%@?!
Wondering how I got customized excerpts on Bleikamp.com? How about the Flickr feed? Maybe you’re wondering how I have the latest post formatted differently than all the others? It’s all easy, I swear.
I’m writing this post for myself more than for my readers. It has been my experience that a lot of people who are new to blogging and, more specifically, WordPress, want to know how I made something work the way I wanted it to. Rather than write the same response over and over I thought it would be best to write a post that explains the design choices, the WordPress hacking, and how I ended up at what you see in front of you
The Design
The design of this site is relatively simple. I wanted people to see the latest content. At some point I’m going to add some more content to the sidebar that will highlight my favorite posts, but until I get a larger archives section that’s not really necessary.
The Plugins
It would be impossible to make a custom designed WordPress blog work “perfectly” without some great plugins. There are a few plugins that I use repeatedly on my designs, and have used again on Bleikamp.com.
1. The Excerpt Reloaded allows me to pick the number of words an excerpt is, it allows me to pick which tags I want to include in my excerpts, and lets me set a number of other options. It’s the perfect solution if you’re looking for control over your excerpts and find that the standard WordPress tag isn’t doing the job.
2. Flickr RSS is a simple plugin that pulls your Flickr RSS feed and can display up to 10 of your latest photos. Not the most powerful Flickr plugin, but it works. I styled the images with CSS to set them up the way that I wanted.
3. Gravatar has an easy to use WordPress plugin to pull people’s Gravatar images in the comments section. Easy to install, easy to copy/paste their code, and BAM! it works.
That’s really all I needed to make this layout work plugin wise. Like I said, nothing too fancy, just a few things to display the content the way I wanted.
The Loop
If you’ve ever worked with WordPress code you know that “the loop” drives everything on the site. If you look at my index page you’ll see I was able to style the first post differently than the following posts. I set the loop up so that the latest content was more “featured” than the older stuff.
With a custom WP query, it was pretty easy to do. First i’ll show the code, then explain what it does.
$feat_count = 0;
$feat_query = new WP_Query('showposts=5');
if($feat_query->have_posts()) { while($feat_query->have_posts() && ($feat_count < 1)) :
$feat_query-the_post(); update_post_caches($posts); ?>
Okay, this is the beginning of my WP loop. I am not a PHP guy, Chris Pearson helped me out a bit with the errors I was getting, but here is what is does:
- I define a variable called “$feat_count.”
- I define a new custom query, $feat_query holds the custom WP query. I assign the number of posts to show - 5 in this case.
- I tell the website “If I have posts, I want to run the custom query I defined, so look $feat_query and print my posts. But Wait! Only do this as long as $feat_count is not more than 1
- Then we do some PHP caching madness that I don’t quite understand but it works so I’m not going to mess with it much.
After this bit of PHP code I type the HTML for the latest post.
Next:
I need to tell tell the server to keep track of the number of posts being printed, so after the post I increase the $feat_count by 1.
But wait, there are still 4 posts waiting to be printed, the custom query remembers this - I told it I wanted 5 posts, not 1. So we go ahead and call the rest of the posts.
if ($feat_query->have_posts()) { while($feat_query->have_posts()) : $feat_query->the_post(); update_post_caches($posts);
In this bit of PHP I’m saying “as long as I still have posts, print 5 of them”. After this bit of PHP I would type the HTML that I want to style the remaining 4 posts in the query. This is also where I use The Excerpt Reloaded, to keep these excerpts short and avoid the standard WP excerpt look.
endwhile; }
End the loop, avoid PHP errors, everyone is happy.
This loop is a bit heavy duty for what I’m doing on Bleikamp.com, I actually stole it from my Lovely Blogs code. I like it a lot though, because in theory I could keep limiting each bit of HTML to 1 post and style 100 posts in 100 different ways.
If you’re looking for something a little more simple there is a great “Featured post” loop example at the WordPress codex that lets you limit the featured post to a specific category and not repeat the featured post in the rest of the content. You could do that with my loop, too, but the one at the codex is a little easier to use in my opinion.
Tying up some loose ends
After I had the index page designed I setup the footer, setup my pages, and made everything work together. I am not going to explain each step, but if you want to know anything else all the details are available at the WordPress Codex.
If you have any questions about how I did something, let me know.
1 response so far ↓
16 Aug 2007 at 2:33 am
Can you give the code surrounding that PHP bit there? Or more specifically where in the loop is this?
Leave a Comment