0

I am attempting to exclude all sticky posts from my front page in twenty twelve theme. I have attempted the following. I am making all changes to the child theme.

  1. To remove the following code from content.php

    <?php if ( is_sticky() && is_home() && ! is_paged() ) : ?>
    <div class="featured-post">
        <?php _e( 'Featured post', 'twentytwelve' ); ?>
    </div>
    <?php endif; ?>
    
  2. Add following code to index.php before the loop(this removed sticky posts but broke pagination)

    <?php query_posts( array( 'post__not_in' => get_option( 'sticky_posts' ) ) ); ?>
    

This is the loop in index.php (pretty much standard index.php for twenty twelve.

        <?php while ( have_posts() ) : the_post(); ?>
            <?php get_template_part( 'content', get_post_format() ); ?>



        <?php endwhile; ?>


        <?php twentytwelve_content_nav( 'nav-below' ); ?>

Could someone please advise me on what I should exactly put into this loop to not show any sticky posts on index.php?

Thank you for your time.

1 Answer 1

4

Don't use query_posts. Use a filter on pre_get_posts.

function no_front_sticky_wpse_98680($qry) {
  if (is_front_page()) {
    $qry->set('post__not_in',get_option( 'sticky_posts' ));
  }
}
add_action('pre_get_posts','no_front_sticky_wpse_98680');

By running query_posts you clobber the main query, over-writing it with another query. That is why you break pagination. The new query gets out of sync with what should be the main query.

3
  • I assume you would add this above to functions.php, but would I need to add anything inside the loop of index.php such as <?php no_front_sticky_wpse_98680() ?>
    – UkrVolk11
    Commented May 8, 2013 at 3:50
  • Just add it to functions.php. Nothing else is required. That will modify the main query for the front page.
    – s_ha_dum
    Commented May 8, 2013 at 4:12
  • @UkrVolk11 : if this solved the problem please mark it as "Accepted"
    – s_ha_dum
    Commented May 8, 2013 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.