Dirty Bird Design
Dirty Bird Design

Reputation: 5523

WP pagination not working

Im trying to add pagination to a page, but I can't even limit the number of posts shown. This is a strange issue, in the Settings -> reading I have posts per page set to 2, with no effect. Here is my query and loop:

<?php 
    $args = array(
            'post_type'         => array( 'webinar' ),
            'post_status'       => array( 'publish' ),
            'posts_per_page'    => '2',
            'order'             => 'DESC',
            'orderby'           => 'date',
            'tax_query' => array(
                    array(
                        'taxonomy' => 'webinar-status',
                        'field' => 'id',
                        'terms' => 178
                    )
                )
            );

            // The Query

            $archived_webinar_query = new WP_Query( $args ); $counter = 1;

            // The Loop
            if ( $archived_webinar_query->have_posts() ) {
                while ( $archived_webinar_query->have_posts() ) {
                    $archived_webinar_query->the_post(); ?>
                    <div class="classes<?php if ($counter % 2 == 0){ echo('f-right l-nmr'); } ?>">
                        <a href="<?php the_permalink(); ?>" target="_self" class="c-ltBlue"><?php the_title(); ?>
                        //other stuff here
                    </div>
                <?php $counter++ ;}
                // Restore original Post Data
                wp_reset_postdata();
            } else { ?>
                <p class="l-twelve l-mb1 f-reg c-gray f-size16 f-l-height24">No archived webinars are available at this time.</p>
            <?php }


        ?>

Upvotes: 0

Views: 53

Answers (2)

Junaid
Junaid

Reputation: 1300

As per my comment, try 'posts_per_page' => 2, it's an integer type.

Your code should be

<?php 
    $args = array(
        'post_type'         => array( 'webinar' ),
        'post_status'       => array( 'publish' ),
        'posts_per_page'    => 2,
        'order'             => 'DESC',
        'orderby'           => 'date',
        'tax_query' => array(
            array(
                'taxonomy' => 'webinar-status',
                'field' => 'id',
                'terms' => 178
            )
        )
    );

    // The Query

    $archived_webinar_query = new WP_Query( $args ); $counter = 1;

    // The Loop
    if ( $archived_webinar_query->have_posts() ) {
        while ( $archived_webinar_query->have_posts() ) {
            $archived_webinar_query->the_post(); ?>
            <div class="classes<?php if ($counter % 2 == 0){ echo('f-right l-nmr'); } ?>">
                <a href="<?php the_permalink(); ?>" target="_self" class="c-ltBlue"><?php the_title(); ?>
                //other stuff here
            </div>
        <?php $counter++ ;}
        // Restore original Post Data
        wp_reset_postdata();
    } else { ?>
        <p class="l-twelve l-mb1 f-reg c-gray f-size16 f-l-height24">No archived webinars are available at this time.</p>
    <?php }

?>

Edit: Improvements

You do not need a counter variable. WP_Query object provides a property for this. In your case, $archived_webinar_query->current_post will give you index for the current post in loop.

Reference to documentation

Upvotes: 1

Mo&#39;men Mohamed
Mo&#39;men Mohamed

Reputation: 1909

I use this function to add numeric pagination, and it's working with me.

I posted it to you maybe help you :)

Add this function to your functions.php

function theme_pagination($pages = '', $range = 3)
{

      global $wp_query;
      if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
      elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
      else { $paged = 1; }

      $showitems = ($range * 2)+1;  

      if(empty($paged)) $paged = 1;

      if($pages == '' && $pages != 0)
      {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   

     if(1 != $pages)
     {
         echo "<div class='pagination'>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
         echo "</div>\n";
     }

}

add this to the top of the loop global $wp_query;

and use theme_pagination($wp_query->max_num_pages); after wp_reset_postdata();

Upvotes: 1

Related Questions