0

I am using following code for query post but I am not getting pagination link. Where is the problem of the following code.

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

    <?php 

        $arr = array(
            'post_type'     => 'post',
            'posts_per_page' => 3
        );

        $the_query = new WP_Query($arr);
     ?>

    <?php if ( $the_query->have_posts() ) : ?>

        <?php if ( is_home() && ! is_front_page() ) : ?>
            <header>
                <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
            </header>
        <?php endif; ?>

        <?php
        // Start the loop.
        while ( $the_query->have_posts() ) : $the_query->the_post();
            get_template_part( 'content', get_post_format() );

        // End the loop.
        endwhile; ?>
        <div class="nav-previous alignleft"><?php  next_posts_link('Older Post'); ?></div>
        <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
    <?php

    wp_reset_postdata();

    endif;
    ?>

    </main><!-- .site-main -->
</div><!-- .content-area -->
0

I go the solution. I have take variable name like $wp_query = new WP_Query($arr); instate of $the_query = new WP_Query($arr); So my working code should be

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

    <?php 
        // global $wp_query;

        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

        $arr = array(

            'post_type'     => 'post',
            'posts_per_page' => 3,
            'paged' => $paged
        );

        $wp_query = new WP_Query($arr);
     ?>

    <?php if ( $wp_query->have_posts() ) : ?>

        <?php if ( is_home() && ! is_front_page() ) : ?>
            <header>
                <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
            </header>
        <?php endif; ?>

        <?php
        // Start the loop.
        while ( $wp_query->have_posts() ) : $wp_query->the_post();
            get_template_part( 'content', get_post_format() );

        // End the loop.
        endwhile; ?>
        <div class="nav-previous alignleft"><?php  next_posts_link('Older Post'); ?></div>
        <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
    <?php

    wp_reset_postdata();

    endif;
    ?>

    </main><!-- .site-main -->
</div><!-- .content-area -->
  • But there is another problem. If I use this custom template as static home page pagination is not working. Anybody can help me? – webHasan Apr 5 '15 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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