Lisa Sabin-Wilson. Designer. Author. Espresso and WordPress addict

post_class(); … Take advantage of the new “Sticky Post” feature in WordPress 2.7

on: Nov/12/08 and its been viewed 9,467 times

Of the many excellent new features coming in WordPress 2.7, some are applauding the arrival of sticky posts.

First – what are they? Sticky posts are posts that you can “stick” to your front page. For instance, if you have a post that you published a year ago and would like to republish it to the front page of your site for a time – you can edit that post and select “Stick this post to the front page” in the Edit Post page in your WordPress admin panel. Just doing that will stick it to your front page, ahead of all the other posts on your site.

You can use CSS to style those sticky posts, as well. All you need is this markup:

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">

That will insert a class=”sticky” above your post and you can use CSS to style .sticky. Cool, eh?

More? Yes, there is more. That same tag will insert special classes for your categories and tags, as well. An example?? I’m glad you asked! Hypothetically, I have a sticky post that I’ve filed in the Misc category and tagged with News, WordPress and Markup. With me so far? With all of that, plus the post_class(); tag in my template – my source code for that particular post looks like this:

<div class="post sticky hentry category-misc tag-news tag-wordpress tag-markup" id="post-ID">

Now, I can use CSS to create special styles for the following classes:
– .post
– .hentry
– .sticky
– .category-misc
– .tag-news
– .tag-wordpress
– .tag-markup

As an example, the code in your index template (and/or single/archive templates) may look like this:

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h3 class="title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Article about: <?php the_title(); ?>"><?php the_title(); ?></a></h3>
<?php the_content('<span class="more-link">'.__('Read More &raquo;').'</span>'); ?>
</div>
<?php endwhile; else: ?>
<?php _e('Sorry, no posts matched your criteria.'); ?>
<?php endif; ?>

I forsee lots of people having lots of fun with this! I am doing some custom work for some clients 2.7 (beta) right now, with the intention to launch them once 2.7-final is released. I’m having good fun working with the new post_class(); tag and the new features of sticky posts, threaded and paged comments. I’m excited for WordPress to release 2.7-final into the wild!

Posted on:November 12, 2008

Posted in: Blog Design, CSS, Design, WordPress, Wordpress Themes

Tags: , , , ,

/*--- 25 Responses to “post_class(); … Take advantage of the new “Sticky Post” feature in WordPress 2.7” ---*/

  1. Danalyn
    2:02 am on November 12th, 2008

    It’s about friggin’ time!

  2. Lisa
    2:11 am on November 12th, 2008

    I know, right? yahoo!

  3. Nathan Rice
    9:46 am on November 12th, 2008

    Lisa,
    Not to try to pimp out my own content, but I really do thing the post I wrote yesterday would be a worthwhile read and/or bookmark for you and your readers.
    http://www.nathanrice.net/blog/definitive-sticky-posts-guide-for-wordpress-27/

    I tried to cover everything I could about the upcoming Sticky Post feature.

    Thanks!
    Nathan

  4. Lisa
    2:23 pm on November 12th, 2008

    Nathan – - of course you meant to pimp your own content!
    That’s cool because your post is v. helpful – thanks for sharing it!:)>-

  5. TheFrosty
    2:51 pm on November 12th, 2008

    I am going to play around till the release, and hopefully get my new site built for the WP community looking pretty in 2.7!

  6. Kristina Brooke
    8:58 pm on November 22nd, 2008

    I am using 2.63 and I just installed a sticky-plugin that is less than great. So, when I read this I almost jumped for joy. This is a much needed addition.

  7. Rob Brennan
    10:55 am on November 24th, 2008

    Hi there! That’s so cool a second edition of your book is coming out; any chance I could buy a signed copy perhaps?

    Have a great Turkey Day – and keep it phunkay!

  8. Barbara Ortagus
    4:00 pm on December 3rd, 2008

    Sure seems like a more practical solution than continuously changing the date of the post, which is what one of my friends does.

  9. Beu
    7:31 pm on December 19th, 2008

    Those snow flakes are annoying.

  10. Lisa
    7:45 pm on December 19th, 2008

    Thanks for the feedback, Beu – I happen to like ‘em. :)

    Happy holidays!

  11. 10 ways to make your wordpress theme look like pro. » GoSee-Design › lifestory of a freelance team
    4:25 pm on July 7th, 2009
  12. 10 ways to make your wordpress theme look like pro. – jpablobr.com
    3:19 am on July 8th, 2009
  13. Daniel Balfour
    6:09 am on July 21st, 2009

    Really great post! I’ll even overlook the Pink!

  14. 10 Useful WordPress Coding Techniques « Smashing Magazine
    8:22 am on October 20th, 2009

    [...] Take advantage of the new post class [...]

  15. 10 Useful WordPress Coding Techniques « Tech7.Net
    9:23 am on October 20th, 2009

    [...] Take advantage of the new post class [...]

  16. 10 Useful WordPress Coding Techniques | Search Engine Optimisation
    3:23 pm on October 22nd, 2009

    [...] Take advantage of the new post class [...]

  17. 10 Useful WordPress Coding Techniques – My Facebook
    12:40 am on October 26th, 2009

    [...] Take advantage of the new post class [...]

  18. Web Design News » 10 Useful WordPress Coding Techniques
    1:04 am on October 26th, 2009

    [...] Take advantage of the new post class [...]

  19. WordPress Coding Techniques : Style Posts Individually
    4:12 pm on November 5th, 2009

    [...] Source: Take advantage of the new post class [...]

  20. Robert
    5:09 pm on December 7th, 2009

    Hey I was wondering how to create a unique style for a post and call that style with the post_class() or post_ID(). I don’t want to edit the .post style because it would change the whole layout of the posts, I just want to change the text alignment to left on the single post page. Thanks!
    *Note: The website I listed is my own website, and not the wordpress one that I’m working on

  21. Willans
    10:41 am on December 8th, 2009

    Remember, you can take this even further by adding your own tags in to the post_class, such as:

    This can lead to all sorts of possibilities:
    - Easier styling for individual posts (to achieve a jasonsantamaria style site perhaps)
    - Different styles for different authors.
    - Different style for each month?

  22. neel
    5:15 am on December 11th, 2009

    Excellent trick. and nice function added by wordpress in 2.8 now we can style post according to the post category or posts tags.

  23. Use WP template post-class() to style posts individually | WebDesign Collection
    8:40 pm on December 17th, 2009

    [...] the same way, you can also give special styling to named categories or tags. Another tutorial talks more about [...]

  24. post_class(); … Take advantage of the new “Sticky Post” feature in WordPress 2.7 | Lisa Sabin-Wilson | WordPress News
    10:14 am on December 30th, 2009

    [...] Link: post_class(); … Take advantage of the new “Sticky Post” feature in WordPress 2.7 | Lisa Sabin-… [...]

  25.   10 Useful WordPress Coding Techniques by Oshoamy
    4:10 am on January 6th, 2010

    [...] Take advantage of the new post class [...]

Leave a Reply

  • Li-sa Sa-bin Wil-son: [lee-suh sey-bin will-sun] - pronoun; 1. A wife, mother & friend; 2. Blog Designer & WebHost; 3. Author of WordPress For Dummies; 4. Scuba diver; 5. Downhill Skier; 6. Am. Photographer; 7. Espresso sipper;
    contact me »
    WordPress For Dummies, author Lisa Sabin-Wilson, Amazon.ComWordPress For Dummies, 2nd Ed. was released February 17, 2009. It covers WordPress.com and WordPress.Org through version 2.7 and includes comprehensive information about tweaking your own WordPress theme, creating your own theme and the basics of what you need to know about basic HTML and CSS...read more »

    BuddyPress For Dummies, author Lisa Sabin-Wilson, Amazon.comBuddyPress is the WordPress platform’s answer to community driven websites. BuddyPress is customizable and can be designed to match the style of your website through the use of CSS and understanding BuddyPress templates. BuddyPress For Dummies is available Feb. 2010 --> pre-order on Amazon today!

    • Think Tank Live – Milwaukee, WI | E.Webscapes Blog Design Studio: ...
    • Jammie J.: Hey woman, just catching up with you, been awhile. :) As...
    • Rosebud: i’m glad to see someone is on my side.i thank you...
    • adoyle: anjul, i believe we have only one fool here and a bloated...
    • MK: :( ( the full moon has been out in hamilton NZ for a week...
    • Anjul: Rosebud and Doyle, It has been a pleasure to make you both...
    • Eclipse: oh and Anjul we dont care that you live in a garbage...
    • Eclipse: hey leave Rosebud alone i dont care what you think. i like...
    • Rosebud: Adoyle, i applaud the way you are handling this arguement...
    • adoyle: suspicious name anjul,a good job you were not around during...
    • Rosebud: you know what?just becuase i like animals and don’t...
    • Anjul: Adoyle, I’m a German and my province doesn’t...
    • Anjul: Rosebud, I am sorry – but not surprised – that...
    • Rosebud: my cat Whiskers did not kill my rats he has caught mice...
    • Kfizz: ahhh this makes so much sence now. i am affected by the...
Get Adobe Flash playerPlugin by wpburn.com wordpress themes