Is there a simple way to push a sticky post class into wordpress template?

As far as I know wordpress uses the sticky class only on the frontpages. To use the sticky class as an identifier I like to have it inlisted generally in an archive loop (as for instances category names).

Is there a simple way to push the class into the archive template?

Related posts

1 comment

  1. This can be done using the built-in WordPress post_class filter.

    Add the below code to your functions.php file (in your theme), and it should add the “sticky” class to posts that are sticky in any archive template.

    // add sticky class on archive templates
    function sticky_archive_class( $classes ) {
        global $post;
        if ( is_sticky( $post->ID ) ) {
            if ( is_archive() ) {
                $classes[] = 'sticky';
            }
        }
    
        return $classes;
    }
    add_filter( 'post_class', 'sticky_archive_class' );
    

    Important Note: This relies on the archive template files being properly coded. If you look at the template, and it does not contain code that looks something like so:

    <div <?php post_class() ?>>
    

    Then the template is wrong, and the code will not work, because there’s nothing to “filter”.

Comments are closed.