Support » Fixing WordPress » Pagination won't display after altering loop

  • Resolved tribzi


    Hi, I’ve altered my loop using pre_get_posts() in my functions and now I can’t get any “prev posts”, “next posts” pagination to display on my index page. I’m using posts_nav_link() in index.php, but on the font-end it doesn’t generate any code and leaves my “.page-navigation” div empty.

    Here is my loop alteration in a snippet from my functions.php:

     * Exclude 'articles' from the Musings page
    function exclude_articles( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-4' );
    add_action( 'pre_get_posts', 'exclude_articles' );

    Here is the loop in my index.php:

    <?php if ( have_posts() ) : ?>
    			<?php /* Start the Loop */ ?>
    			<?php while ( have_posts() ) : the_post(); ?>
    					get_template_part( 'content', get_post_format() );
    			<?php endwhile; ?>
    			<div class="page-navigation">
    				<?php posts_nav_link() ?>
    		<?php else : ?>
    			<?php get_template_part( 'content', 'none' ); ?>
    		<?php endif; ?>

    Here is the output from the page source:

    <article id="post-85" class="...">
    </article><!-- #post-## -->
    <article id="post-80" class="...">
    </article><!-- #post-## -->
    <article id="post-77" class="...">
    </article><!-- #post-## -->
    			<div class="page-navigation">
    		</main><!-- #main -->

    As you can see, no “previous” or “next” links show up.

    Any help is appreciated. Thank you in advance. Also thanks to everyone who ever answers these support forums because I use the archives all the time. Thanks for creating such a fantastic resource.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Could it just be that there are no more posts now that you excluded some?

    What is the posts per page setting at?

    Moderator Jose Castaneda



    Have you tried just using the previous_posts_link and next_posts_link template tags?

    I know it may sound a little strange but how many posts are there and how many are set to be view per page?

    Thanks guys, you were right. I was being silly, I forgot my index page wouldn’t have enough posts to fill the 10 posts per page it’s set at once I excluded my category. Sometimes you just need the obvious pointed out though, thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Pagination won't display after altering loop’ is closed to new replies.