Harness the Power of the WordPress Post Class Function and Style Those Sticky Posts

The sticky post functionality in WordPress uses the post class function introduced in WordPress 2.7 to style your sticky post with css. In order to have your sticky posts styled different than your regular posts, you need to have the post class function in your theme within the post loop.

What is a sticky post and why would I use it?

A sticky post is a post that is plucked out from its publish date and moved to the front of your blog. This will keep the post on your homepage so you can increase the visibility of that post. Sticky posts have many uses–one would be if you were having a giveaway. You could stick that post to your homepage so everyone would see it when they visit your blog. Then, once the contest is over, you remove the sticky post option and the post falls back in line to when it was published.

Adding the post class function to your theme

Within the post loop there should be some code that looks something like what is below:

Rest of loop here

Now some themes use more than one class to style their posts. You could have a setup like the following where you have not only a post class but also a box class. Thankfully for you they have made it very easy to add that class into the post_class function.

Rest of loop here

To update the theme all we need to do is replace class=”post” with our function call. Your updated post loop will now look like.

> Rest of loop here

If you need that extra class like the second example that also uses the box class. All you need to do is pass that class name into the post class function.

> Rest of loop here

That is it for updating the code within your theme.

Adding style to your sticky post

The post class function will automatically add a class of sticky to any post marked as such in the post admin screen. So in your style sheet you will need to add some styles for this new class. Here we just added a different background color to our sticky posts.

.sticky{
	background-color: #c2c2c2;
}

Marking a post as sticky

stick-post-edit-window.png

In WordPress 2.8, make your way to the edit screen for the post you would like to make sticky. You’ll then see a box like the one to the left with the visibilty section closed. Click the edit link and you will now see the checkbox. That’s all it takes to take advantage of sticky posts in WordPress.

Be sure to check out the WordPress Codex to find more info on all the functions available in WordPress.

8 Must-Have WordPress Plugins to Improve SEO, Speed, and Security

Plugins are one of the things that make WordPress great. You can easily extend the functionality of WordPress without having to know much about PHP.

These “Must-Have” plugins are ones that I find myself using over and over again. The base installation of WordPress is an impressive piece of software and with these plugins, you can take it to the next level. Get your WordPress site running faster, backed up, secure, and search engine optimized with these 8 plugins in minutes. Just a little effort on your part to set up these plugins will go along way to improving your blog’s traffic.

All-in-One SEO Pack

Whether or not you are an SEO expert, this is a simple-to-use plugin. The All-in-One SEO Pack plugin optimizes your WordPress blog to help you get the most out of your content.

DB Backup

Download or email your backup with ease. You can backup and restore almost anything stored in your WordPress database.

Google Analyticator

With the ability to add your Google Analytics stats right to your WordPress Dashboard, this plugin is an easy way to track and view all those stats you like to see.

Google XML Sitemaps

It doesn’t get much easier than this to help Google and other search engines find all of your content. Simply install this plugin and generator your XML-sitemap. The plugin will generate a new sitemap every time you add new content.

WP-PageNavi

Add more advanced paging to your WordPress blog without having to know PHP.

WP Security Scan

Scans your WordPress installation for security risks and makes recommendations on how to fix them.

WP Super Cache

Speed kills–well, a lack of speed does anyway. This plugin generates static html files of your WordPress pages and can greatly improve load times.

Yet Another Related Posts Plugin

Helping you increase those page views. This plugin gives you the ability to show your readers related content on your site.

Something I missed? Let me know–I am always on the lookout for a good plugin.