phparrayswordpresszurb-foundation

Changing WordPress Sticky Class Name


Unfortunately, Foundation and WordPress both use the "sticky" class. To avoid that, I've always used a little function that changes the class to 'wp-sticky' for WordPress. However, that function now seems to be applying that class to every post (including non-sticky posts).

function remove_sticky_class($classes) {
    $classes = array_diff($classes, array("sticky"));
    $classes[] = 'wp-sticky';
    return $classes;
}
add_filter('post_class','remove_sticky_class');

Any ideas on how I can change just the sticky class name (and not have it apply to all posts)?

Edit: It does REPLACE 'sticky' with 'wp-sticky' on sticky posts, however, it also ADDS it to posts that are not sticky.


Solution

  • It probably never worked like it was supposed to in the first place. The problem was there was no conditional in place to check if 'sticky' was in the $classes array before adding 'wp-sticky' to the array.

    function remove_sticky_class( $classes ) {
        if ( in_array( 'sticky', $classes, true ) ) {
            $classes = array_diff($classes, array('sticky'));
            $classes[] = 'wp-sticky';
        }
        return $classes;
    }
    add_filter( 'post_class', 'remove_sticky_class' );