WordPress.org

Forums

How to remove Sticky functionality in the loop? (1 post)

  1. robotronik
    Member
    Posted 1 year ago #

    I'd like to show only the two most recent sticky posts at the top of the home page. After that, I'd like to show all the other posts (including older stickies), in their natural order and without any sticky formatting.

    Right now, I run sticky posts through an if_sticky() that reformats the HTML just for sticky posts. Non-sticky posts get a different format.

    I currently have the two most recent sticky posts by running one loop:

    <?php 
    
      // Get the latest 2 sticky posts
      $sticky = get_option( 'sticky_posts' );
      rsort( $sticky );
      $sticky = array_slice( $sticky, 0, 2 );
      $my_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) );
    
      while ($my_query->have_posts()) : $my_query->the_post();
    
    ?> 
    
        <?php get_template_part('templates/content', get_post_format()); ?>
    
    <?php endwhile; ?>

    And then I would like to show the rest of the posts (minus these two stickies) as normal posts below in another loop.

    So far I can show all the other posts, but their stickiness remains, and are still reformatted using the if_sticky() statement.

    <?php 
    
        //Get the other posts
        $sticky = get_option( 'sticky_posts' );
        rsort( $sticky );
        $sticky = array_slice( $sticky, 0, 2 );
    
        $query = new WP_Query( array( 'post__not_in' => $sticky, 'ignore_sticky_posts=1') );
    
        while ($query->have_posts()) : $query->the_post();
      ?>
    
        <?php get_template_part('templates/content', get_post_format()); ?>
    
      <?php endwhile; ?>

    How do I remove the stickiness so they show up as regular posts?

Topic Closed

This topic has been closed to new replies.

About this Topic