Quantcast
Channel: Nathan Rice» wordpress 2.7
Viewing all articles
Browse latest Browse all 4

Definitive Sticky Posts Guide for WordPress 2.7

$
0
0

As the blogosphere is abuzz about all the cool new features in WordPress 2.7, I’ve seen very few (if any) good posts on the new Sticky Posts feature that will allow you to take any story, or stories, you’ve published and place them at the top of your homepage without editing the timestamp.  This new feature will allow you to take posts that you’re especially proud of and display them for all the world to see for as long as you want.

But for many theme and plugin developers, and even users who may want to do a little DIY theme hacking, there is very little documentation for getting the feature set up to do the things you want it to.  That’s where this guide comes in.

Turing a Post into a Sticky Post

picture-1In the 2.7 Post edit panel, over in the publish module, you’ll notice a little checkbox.  When you check this box and publish the post (or save an already published post), that post will then be pulled to the top of your homepage.

Also, it is worth noting that a post can be made “sticky” using the “Quick Edit” panel as well. In WordPress 2.7, simple click the Posts -> Edit link, then click the “Quick Edit” link.

This will bring up a panel that lets you edit common features of a post like its categories, tags, title, slug, date, author, etc.  But it also allows you to change whether or not a post is a sticky post.

picture-2

This is especially handy when you need to “unsticky” a post in order to let another post take it’s place at the top of the homepage.  Imagine how tedious it would be to have to pull up the full Edit Post panel just to “unsticky” a post.  It would get very annoying, so I’m glad to see the developers opted to include this option in the Quick Edit panel as well.

Styling a Sticky Post

If you’re like me, you probably want to have a way to let your readers know that, although the post is at the top of your homepage, it may not necessarily be the latest post you’ve published. Luckily, WordPress 2.7 introduces the new post_class() template tag that will give a class of “sticky” to any post that has been made into a sticky post.  Here’s how you would use it.

(By the way, it would be worth your while to take a look at the Codex page for post_class when it becomes available. The function can really give you some cool flexibility in how different posts are styled. I suppose that’s another post for another day.)

If you’re familiar with the loop, you’ll probably recognize the following code (or some variation of it):

<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_content(); ?>
<?php endwhile; endif; ?>

In order to use the new post_class() template tag, just be sure to wrap the content of each post that the loop outputs in a new div.  The code would looks something like this:

<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div <?php post_class(); ?>>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_content(); ?>
</div>
<?php endwhile; endif; ?>

Depending on what type of post it is, it will output different kinds of stuff.  For instance, if you view the source code for my homepage, you’ll see my posts wrapped in these divs already.  For the post Community, Self-Respect, and Free WordPress Themes, the div class generator created this:

<div class="post hentry category-blog category-general category-planet-wordpress category-wordpress-news tag-elevate-themes tag-free-wordpress-theme tag-free-wordpress-themes tag-wordpress">

Now, if that post had been marked “sticky”, then you would have seen the word “sticky” show up in that list too.  You can use any of those classes to style a post differently, but in this case, we’re just looking to differentiate the sticky posts. By adding a new class declaration in our CSS file, we can make a sticky post look any way we want:

.sticky {
    background: white;
    border: 2px solid black;
}

The possibilities are really endless — it’s all up to you!

Sticky Styling in Other Templates

As far as I know, the sticky feature does not actually affect any other templates besides the homepage post listing.  The only possible exception would be the search results (see the Trac ticket for more information).

However, there is one caveat as pointed out by Michael at WPEngineer.com — sticky posts don’t simply get “pulled to the front of the line”.  In fact, if you’re browsing through someone’s archives, and you are using the post_class() template tag, your sticky posts will get that sticky styling, just as though they were at the top of the homepage.

To remedy this, you’ll want to figure out a way to only apply that special sticky style to sticky posts that are on the homepage.  That means we’ll want to exclude things like category and date archives, page 2 of the blog homepage (example), and any other templates that you might use (tags, single posts, etc.).

In order to accomplish this, you’ll need to figure out a way to add a class or ID to the body tag of your theme.  Darren Hoyt has a good post on how to do that is pretty good, but I’ll try to write up a post later that will teach you how to mimic the post_class function for your body tag.

So, once your body has a class or ID, just modify your CSS a little to reflect your desire for the sticky style to only be applied to the homepage:

#home .sticky {
    background: white;
    border: 2px solid black;
}

Easy enough?

is_sticky() Conditional Tag

Along with the ability to use the post_class() to identifiy sticky posts, you can also use the is_sticky() conditional tag to check to see if a post is sticky or not.  Here’s a practical example (to be used within the loop):

<?php
if (is_sticky()) echo 'This is a sticky post!';
?>

The is_sticky() conditional tag returns a Boolean value of TRUE or FALSE depending on whether or not the post is a sticky post.  Use your imagination to determine the different applications this tag can help you achieve.

A Custom Loop to Return All Sticky Posts

If you’re a hardcore theme developer and want to use a custom loop to return only posts that have been marked as sticky, you can use this very handy code provided by Otto at the Support forums:

query_posts(array('post__in'=>get_option('sticky_posts')));

This code goes before the loop. It qualifies the loop by telling it to only return posts that have been marked “sticky”.  You can use all the other parameters of the query_posts() loop qualifier as well, but that post__in part is the part that actually tells the loop to only return the sticky posts.

A Custom Loop to Ignore Sticky Posts

Generally speaking, sticky posts will show up at the top of ever single loop you use on your homepage.  So for complex themes that use multiple loops on the homepage to return different data, each and every one of those loops will return the same sticky post(s).  And it seems like even loop qualifiers like the query_posts() argument post__not_in don’t work on excluding posts.  No matter what you do, those sticky posts will be at the top of all your loops.

Thankfully, once again Otto comes to the rescue with some more handy code.

If you want your loop to ignore the sticky status of posts, just use this code before the loop:

query_posts('caller_get_posts=1');

Again, this parameter can be used with any of the other arguments for query_posts()And this code doesn’t simply exclude posts marked sticky … it actually ignores the sticky status altogether.  So if a person marks a post as sticky, by using this query_posts() parameter, you can have your loop act normally, not pulling that post to the top unless it is the latest post published (depending on the other query_posts parameters).

Getting Rid of Stickies Completely

Finally, if your theme doesn’t take advantage of the sticky option at all, and you just want to prohibit users from making a post sticky in the first place, you can just wipe out the field in the options table that holds all the sticky post IDs by placing this code in your theme’s functions.php file between <?php ?> tags: [HT: WPEngineer.com]

update_option('sticky_posts', array());

This empties the field and get’s rid of all sticky post IDs that the user may have saved.  This could easily save you the trouble of having to support users that insist on checking the sticky checkbox despite your warnings.  It’s just a way for you to nip the problem in the bud.

However, use it with caution.  Users don’t normally like it when you remove their data.

Well, that about wraps it up.  Everything you need to know about the Sticky Post feature in WordPress 2.7. If you have any other tips or questions, please feel free to let me know in the comments below.  I’d love to hear your thoughts about this new feature.


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images