Close

Adding a Custom Css Class to Posts Sticked by “Category Sticky Post” Plugin

While working on this blog, I needed to set posts as sticky for a specific category. to do that I use the plugin Category Sticky Post which work perfectly for me.

However, my theme use the css class “sticky” for sticky posts but the plugin don’t add it, so I needed to add a small fix into the functions.php file of my child theme.

 

It’s a simple filter hook on “post_class”, here’s the code:


function add_sticky_to_category_sticky($classes) {
if (in_array('category-sticky', $classes))
{
$classes[] = 'sticky';
}
return $classes;
}
add_filter( 'post_class', 'add_sticky_to_category_sticky', 11, 1 );

I look for the plugin specific css class and if found, I simply add mine.

 

The only unknown here is if my hook will always be called after the plugin or not 😉

Edit: after a fast google search, I added priority to the hook, the plugin use the default value of ten, so I used 11.

Leave a Reply

Your email address will not be published. Required fields are marked *