最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

next post link - next_posts_link not working in custom loop

programmeradmin0浏览0评论

I've created a custom loop for posts, that works with Bootstrap, in which, I've added pagination - however it doesn't seem to be working. It just shows an empty button...

<?php $args = array(
         'post_type' => 'post',
         'posts_per_page' => 1
    );

    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
        $i = 0;
        while ($my_query->have_posts()) : $my_query->the_post();
            if($i % 2 == 0) { ?>
            <div class="row"><?php } ?>
                <div class="col-md-6">
                    <div class="overview">
                       <!-- Post details here -->
                    </div>
                </div>
                <?php $i++; if($i != 0 && $i % 2 == 0) { ?>
            </div><!--/.row-->
            <div class="clearfix"></div>
                <?php }
            endwhile; ?>
            <nav class="pagination">
                <button class="btn-whole"><?php previous_posts_link('Previous') ?></button>
                <button class="btn-whole"><?php next_posts_link('Next') ?></button>
            </nav>
    <?php } wp_reset_query(); ?>

Any ideas on why this might be happening?

I've created a custom loop for posts, that works with Bootstrap, in which, I've added pagination - however it doesn't seem to be working. It just shows an empty button...

<?php $args = array(
         'post_type' => 'post',
         'posts_per_page' => 1
    );

    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
        $i = 0;
        while ($my_query->have_posts()) : $my_query->the_post();
            if($i % 2 == 0) { ?>
            <div class="row"><?php } ?>
                <div class="col-md-6">
                    <div class="overview">
                       <!-- Post details here -->
                    </div>
                </div>
                <?php $i++; if($i != 0 && $i % 2 == 0) { ?>
            </div><!--/.row-->
            <div class="clearfix"></div>
                <?php }
            endwhile; ?>
            <nav class="pagination">
                <button class="btn-whole"><?php previous_posts_link('Previous') ?></button>
                <button class="btn-whole"><?php next_posts_link('Next') ?></button>
            </nav>
    <?php } wp_reset_query(); ?>

Any ideas on why this might be happening?

Share Improve this question asked Apr 17, 2018 at 14:18 Jake PuntonJake Punton 392 silver badges9 bronze badges 1
  • codex.wordpress/Function_Reference/… – Michael Commented Apr 17, 2018 at 21:36
Add a comment  | 

3 Answers 3

Reset to default 2

only just use echo and get_

<?php $args = array(
     'post_type' => 'post',
     'posts_per_page' => 1
);

$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
    $i = 0;
    while ($my_query->have_posts()) : $my_query->the_post();
        if($i % 2 == 0) { ?>
        <div class="row"><?php } ?>
            <div class="col-md-6">
                <div class="overview">
                   <!-- Post details here -->
                </div>
            </div>
            <?php $i++; if($i != 0 && $i % 2 == 0) { ?>
        </div><!--/.row-->
        <div class="clearfix"></div>
            <?php }
        endwhile; ?>
        <nav class="pagination">
            <button class="btn-whole"><?php echo get_previous_posts_link('Previous'); ?></button>
            <button class="btn-whole"><?php echo get_next_posts_link('Next'); ?></button>
        </nav>
<?php } wp_reset_query(); ?>

I know this post is a bit old but I got stuck in the same point and probably other will stumble upon in question to find an answer. I wanted to create a custom loop in the file index.php and I solved using the documentation @Michael posted in the comment. I'm just converting it to a solved answer.

<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

$args_blog = array(
            'post_type' => 'post',
            'posts_per_page' => 9,
            'category__in' => array( 2, 3 ),
            'paged' => $paged
          );

// the query
$the_query = new WP_Query( $args_blog ); 
?>

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

<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post(); 
?>
<?php the_title(); ?>
<?php endwhile; ?>

<?php

// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>

<?php 
// clean up after the query and pagination
wp_reset_postdata(); 
?>

<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
try now this code
<? 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
   $args = array(
         'post_type' => 'post',
         'posts_per_page' => 1,
         'paged' => $paged,
    );

    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
        $i = 0;
        while ($my_query->have_posts()) : $my_query->the_post();
            if($i % 2 == 0) { ?>
            <div class="row"><?php } ?>
                <div class="col-md-6">
                    <div class="overview">
                       <!-- Post details here -->
                    </div>
                </div>
                <?php $i++; if($i != 0 && $i % 2 == 0) { ?>
            </div><!--/.row-->
            <div class="clearfix"></div>
                <?php }
            endwhile; ?>
            <nav class="pagination">
                <button class="btn-whole"><?php previous_posts_link('prev', $my_query->max_num_pages); ?></button>
                <button class="btn-whole"><?php next_posts_link('next', $my_query->max_num_pages); ?></button>
            </nav>
    <?php } wp_reset_query(); ?>
发布评论

评论列表(0)

  1. 暂无评论