• I’m using WP_QUERY to modify post loops. Post loops work correctly in index.php, archive.php category.php, but if I add a post loop on a page, the post navigation doesn’t display.

    Here is what I did:

    1. in admin>pages>add new create a page called test1.
    – create the file for it in theme root page-test1.php
    – In that file add the post loop code, modified with WP_QUERY:

    <?php get_header(); ?>
    
    <div class="content" id="content">
        <main id="main" class="site-main">
    
            <?php
    
            $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
            $args = array(
    
                'post_type' => 'post',                  
                'paged' => $paged,
                'posts_per_page' => 2,
            );
            $the_query = new WP_Query( $args ); 
    
            ?>
    
            <?php if ( $the_query->have_posts() ) : ?>
    
                <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
                    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
                        <div class="post-header">
    
                            <h2 class="post-title">
                                <?php the_title(); ?>
                            </h2>
    
                        </div>
    
                        <div class="post-excerpt">
    
                            <?php the_excerpt(); ?>
    
                        </div>
    
                    </div>
    
                <?php endwhile; ?>
    
                    <div class="pagination">                 
                        <?php the_posts_navigation(); ?>
                    </div>
    
                <?php wp_reset_postdata(); ?>
    
            <?php else : ?>
    
                <p><?php esc_html_e( 'No results', 'hps' ); ?></p>
    
            <?php endif; ?>
        </main>
    </div>
    
    <?php get_footer(); ?>

    Does anyone know how to get the post navigation working for a post loop on a page?
    Any help appreciated.

    • This topic was modified 8 years, 4 months ago by Andrew.
    • This topic was modified 8 years, 4 months ago by Andrew.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Post loop on a page’ is closed to new replies.