0

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 -->
    <?php
    $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(); ?>
        <h2>
            <a href="<?php the_permalink(); ?>"><?php the_title();?></a>
        </h2>
        <p class="lead">
            <?php $author = get_the_author(); ?>
            by <?php echo $author ?>
        </p>
        <p><i class="fa fa-clock-o"></i> Posted on <?php the_time('F j, Y'); ?></p>
        <hr>
        <a href="#">
            <?php if( get_field('image') ): ?>
            <img class="img-responsive img-hover" src="<?php the_field('image'); ?>" alt="">
            <?php endif; ?>
        </a>
        <hr>

        <?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>

        <hr>


    <?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>
            <li class="next">
                <a href="<?php echo get_previous_posts_link( 'Newer Entries' ); ?>">Newer &rarr;</a>
            </li>
        </ul>
        <?php } ?>

    </div>
0

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.