-1

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?

1

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

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.