Posted on 28 January, 2009 By Kim Woodbridge 31 Comments

WordPress 2.7 Post_Class Template Tag and Sticky Posts

message for you sir (by Ryan Grove)

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


Related Posts:
  • Using Conditionals with WordPress 2.7 Sticky Posts
  • How to Style a Sticky Post in WordPress 2.7
  • How to Make a Post Sticky in WordPress 2.7
  • Using WordPress to Help Manage Your Freelance Business
  • How to Remove “Says” From WordPress 2.7 Threaded Comments
  • Posted In : WordPress Tips
    If you enjoyed this post, please subscribe to my RSS Feed

    Post to Twitter   Post to Delicious   Post to StumbleUpon

    31 Responses to “WordPress 2.7 Post_Class Template Tag and Sticky Posts”

    Trackbacks/Pingbacks
    1. Friday Night Links Party - 30 January 2009 | Nihar's World
    Leave a Comment
    You may use: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .


    Recent Comments

      • Kim Woodbridge: Hi Dean - Thanks! I'm just glad I got the punctuation right - I've been having a rough week with typos ......
      • Kim Woodbridge: Hi Jim - What's not to love about Mr T?...
      • Kim Woodbridge: Hi - Thanks for visiting! It is funny :-)...
      • Dean Dwyer: Ah the power of the comma. It's April, Fools. Who knew punctuation could be so funny. Thanks for the smile....
      • Kim Woodbridge: Hi Christine - Awesome! Ooh ... something shiny ;-)...
      • Kim Woodbridge: Hi - I wish Twitter lists had started sooner. I've worked on them some but I'm having a hard time finding the time to w...
      • Christine: This has been driving me crazy for... maybe forever! I created too many lists and deleting them was not exactly clear -...
      • Ching Ya: Honestly I can't think of a way to live without lists. :-) Twitter lists, Facebook lists.. all of these have helped me a...