I am trying to paginate my custom post to have multiple pages once a limit of posts is met per page. The end goal is to have the url have /page=2 etc. I have set the limit to one post per page to test (currently have two posts) but the old and new buttons at the bottom are not showing up? What am I doing wrong?

    <div class="col-md-8">

    <!-- set up or arguments for our custom query -->
    $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
    $query_args = array(
        'post_type' => 'news',
        'posts_per_page' => 1,
        'paged' => $paged
    ); ?>

    <?php $news = new WP_Query(array(
            'post_type' => 'news'
        ));  ?>
    <?php while($news->have_posts()) : $news->the_post(); ?>
            <a href="<?php the_permalink(); ?>"><?php the_title();?></a>
        <p class="lead">
            <?php $author = get_the_author(); ?>
            by <?php echo $author ?>
        <p><i class="fa fa-clock-o"></i> Posted on <?php the_time('F j, Y'); ?></p>
        <a href="#">
            <?php if( get_field('image') ): ?>
            <img class="img-responsive img-hover" src="<?php the_field('image'); ?>" alt="">
            <?php endif; ?>

        <?php if(get_field('news_content')): ?>
            <p class="description"><?php echo get_field('news_content'); ?></p>
        <?php endif; ?>

        <a class="btn btn-primary" href="<?php the_permalink(); ?>">Read More <i class="fa fa-angle-right"></i></a>


    <?php endwhile ?>

        <!-- Pager -->
        <?php if ($news->max_num_pages > 1) { ?>
        <ul class="pager">
            <li class="previous">
                <a href="<?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); ?>">&larr; Older</a>
            <li class="next">
                <a href="<?php echo get_previous_posts_link( 'Newer Entries' ); ?>">Newer &rarr;</a>
        <?php } ?>


try to adapt the code from the Codex https://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query to fit your own query:

        <a href="<?php echo get_next_posts_link( 'Older Entries', $news->max_num_pages ); ?>">&larr; Older</a>

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.