Posted on 28 January, 2009 By 33 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:

Posted In : WordPress Tips

Did you enjoy this article? I can assist you with your WordPress or Facebook project. Contact me and I will provide you with an estimate. You can also connect with me on Facebook or follow me on Twitter.




Premise Landing Pages Made Easy

33 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 badge

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.

Recent Comments

    • Marbella: Hi Kim, To get a Valentine card from a child who has been working with it for a few days means so much. They show their...
    • Gaynell M. Jones: Hi Kim, Wow, This is amazing! How can you read this much and blog at the same time?.. Well I'm not that a reader myse...
    • Gaynell M. Jones: Wow! This are cute! I will creat this for my darling. Thanks for the sharing this wonderful Idea....
    • Cmack: I found this website that shows how to have thumbnails link to another page by using the alt/title info in the gallery. ...
    • Lily Rose: This is so cute! I love it when people make their day and do the same for their close ones...

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