WP分页不起作用

时间:2017-08-22 15:44:02

标签: wordpress loops pagination

我正在尝试为页面添加分页,但我甚至无法限制显示的帖子数量。这是一个奇怪的问题,在设置 - >阅读我每页的帖子设置为2,没有效果。这是我的查询和循环:

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


        ?>

2 个答案:

答案 0 :(得分:1)

我使用此功能添加数字分页,并且它与我合作。

我发给你的可能会帮助你:)。

将此功能添加到你的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";
     }

}

将其添加到循环顶部global $wp_query;

并在theme_pagination($wp_query->max_num_pages);

之后使用wp_reset_postdata();

答案 1 :(得分:1)

根据我的评论,请尝试'posts_per_page'=&gt; 2,它是一个整数类型。

您的代码应为

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

?>

修改:改进

您不需要计数器变量。 WP_Query对象为此提供了一个属性。在您的情况下,$archived_webinar_query->current_post将为您提供循环中当前帖子的索引。

Reference to documentation