On Monday I showed you where the sticky post setting was. Maybe you already knew but I had quite a time locating it and thought I might not be the only one having this problem.
So, you used that setting to make a post sticky and now you’re wondering why it isn’t working properly. Well, you have to add some code to your Main Index template.
WordPress 2.7 includes a new function called Post Class, which makes it easier to style certain parts of the theme. One of the classes used with post class is sticky. So, the template doesn’t know that a sticky post even exists until this new code is added.
You need to add the following code within the Loop. Make sure it is somewhere that makes sense.
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> Rest of loop here </div>
Many templates already have code like the following:
<div class="post"> Rest of loop here </div>
If your template has the above code, you can simply replace is with the new post class code. If it doesn’t, I’ve found you can add the new code and leave the code you already have.
If you are now wondering what the loop is, it is the code that makes all of your posts show up on the index page.
In it’s most basic form it looks like the following:
<?php if(have_posts()) : ?> <?php while(have_posts()) : the_post(); ?> // Custom Code Here <?php endwhile; ?> <?php else : ?> <?php endif; ?>
Adding the post class code to this will then look like:
<?php if(have_posts()) : ?> <?php while(have_posts()) : the_post(); ?> <div <?php post_class(); ?>> // Custom Code Here </div> <?php endwhile; ?> <?php else : ?> <?php endif; ?>
The new post class div has been added after the while have posts code and the end div is after the else code.
Adding this will make your sticky posts show up properly. If you want to style the sticky post so it looks different than the rest of the posts, make sure you check back on Thursday for my next article.
If you would like to add this to your templates, but don’t want to do it yourself, contact me and hire me to do it for you. It’s a quick and affordable job.
And finally, if you want to read more about the post class, there is information in the WordPress codex.
photo credit: Ryan Grove