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?

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(); ?>
  • This still doesn't work... – Jake Punton Apr 17 '18 at 17:26
  • it's work on post single page in.. – NikHiL Gadhiya Apr 18 '18 at 6:46
  • I'm editing the index.php file. – Jake Punton Apr 18 '18 at 10:07
0
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(); ?>
  • This bring up a link to the next page, however, when going to the second page it brings back a 404 error. Any ideas? – Jake Punton Apr 18 '18 at 10:06
  • This has nothing to do with what I am asking. The second page is resulting in a 404 and I need to use the same or similar loop as above. – Jake Punton Apr 20 '18 at 15:16
0

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

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.