Posted on 28 January, 2009 By Kim Woodbridge 31 Comments

WordPress 2.7 Post_Class Template Tag and Sticky Posts

 
Share

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. You can also connect with my Facebook page or follow me on Twitter.



    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> .

    CommentLuv Enabled

    Recent Comments

      • Ari Herzog: Agreed on the updates/inbox snafu. I rarely see page updates and I know where they are! .-= Ari Herzog´s last blog .....
      • jayson: Kim, Love the themes. I am looking to update the theme on my site to something more appropriate. My site is all abou...
      • vered: "I wish more people would use this. I think it’s silly when I get updates for things occuring in Kansas and California –...
      • Dennis Edell @ Direct Sales Marketing: I was speaking generally, not specifically this month. how exactly do you work things? .-= Dennis Edell @ Direct Sales M...
      • Lucy Beer: Unfortunately I think next to no-one actually sees those "Updates". They're buried in a subsection of the inbox and I do...
      • Kim Woodbridge: Hi Marbella - Thanks! I had a great time....
      • Kim Woodbridge: Hi Julie - I'll probably try kayaking again next summer but I don't know if I will become much of a kayaker - I had a di...
      • Kim Woodbridge: Hi Vered - If you ever get the chance, you should go. It's like a different world - it's so quiet and peaceful....

    About

    Kim Woodbridge is an accomplished Information and Technical Consultant specializing in the entire implementation of a WordPress based website including installation, theme design, upgrades, unique customizations and ongoing site maintenance.

    Wordpress Services

    • Installation, upgrades and maintenance
    • Conversion of existing html and css templates
    • Theme and plugin recommendations
    • CSS customizations
    • Troubleshooting and tweaks for unique situations
    • Customization for individual blogging goals and needs
    • Training and advice