WordPress.org

Forums

[resolved] Pagination won't display after altering loop (4 posts)

  1. tribzi
    Member
    Posted 11 months ago #

    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(); ?>
    
    				<?php
    					get_template_part( 'content', get_post_format() );
    				?>
    
    			<?php endwhile; ?>
    
    			<div class="page-navigation">
    				<?php posts_nav_link() ?>
    			</div>
    
    		<?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">
    			</div>
    
    		</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.

  2. Pioneer Valley Web Design
    Member
    Posted 11 months ago #

    Could it just be that there are no more posts now that you excluded some?

    What is the posts per page setting at?

  3. Jose Castaneda
    THEME COFFEE MONKEY
    Posted 11 months ago #

    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?

  4. tribzi
    Member
    Posted 11 months ago #

    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.

Reply

You must log in to post.

About this Topic