【问题标题】:Implementing pagination in wp doesn't work在 wp 中实现分页不起作用
【发布时间】:2016-09-10 22:51:39
【问题描述】:

我正在尝试向我的 wordpress 主题添加一些代码,以在帖子底部显示分页。 这是我的分页循环:

<main id="main">

            <?php 
            // the query
            $args = array('posts_per_page' => 2 );
            $the_query = new WP_Query( $args ); 

            ?>

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

                <!-- loop -->

                <?php while ( $the_query->have_posts() ) {

                            $the_query->the_post(); ?>
       <article id="post"> 

                    <div id="thumbnail">

                        <?php
                        if ( has_post_thumbnail() ) {
                             the_post_thumbnail(); } ?>

                </div>

               <h2><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h2>

               <div class="entry">

                    <?php the_excerpt(); ?>

               </div>


       </article>


            <?php } } else { ?>
            <p><?php _e( 'Die Posts entsprechen nicht den Kriterien.' ); ?></p>
            <?php }  ?>

            <!-- pagination -->
                <?php
    if($the_query->max_num_pages>1){?>
        <p class="paged">
        <?php
          if ($paged > 1) { ?>
            <a href="<?php echo '?paged=' . ($paged -1); //prev link ?>"><</a>
                            <?php }
        for($i=1;$i<=$the_query->max_num_pages;$i++){?>
            <a href="<?php echo '?paged=' . $i; ?>" <?php echo ($paged==$i)? 'class="selected"':'';?>><?php echo $i;?></a>
            <?php
        }
        if($paged < $the_query->max_num_pages){?>
            <a href="<?php echo '?paged=' . ($paged + 1); //next link ?>">></a>
        <?php } ?>
        </p>
    <?php } ?>
<!--  end pagination -->    


           <!-- end of the loop -->


           <?php wp_reset_postdata(); ?>


    </main>

当我查看源代码时,我找不到分页。我究竟做错了什么?找不到答案,没有代码适合我。如果有人可以帮助我,那就太好了。 :)

【问题讨论】:

    标签: php html wordpress web wordpress-theming


    【解决方案1】:

    如果您希望分页包含页面链接,格式为 而不仅仅是下一个/上一个链接,您可以使用 WordPress 函数paginate_links

    因此,对于您的示例,代码如下所示:

    <?php 
    //query
    $paged = (isset($_REQUEST['paged']) && $_REQUEST['paged'] > 0 ? $_REQUEST['paged'] : max( 1, get_query_var('paged') ));
    $args = array(
        'posts_per_page' => 2,
        'paged' => $paged,
    );
    $the_query = new WP_Query( $args );
    
    //loop
    if ($the_query->have_posts()) {
        while ($the_query->have_posts()) {
            $the_query->the_post();
    
            //display your post here
        }  
    
        //pagination
        if($the_query->max_num_pages > 1){ ?> 
            <div class="paged">
                <?php 
                $big = 999999999; // need an unlikely integer
    
                echo paginate_links( array(
                    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                    'format' => '?paged=%#%',
                    'current' => $paged,
                    'total' => $the_query->max_num_pages,
                    'prev_text' => __('« Previous'),
                    'next_text' => __('Next »'),
                ) ); 
                ?> 
            </div> 
        <?php }
    
    } else {
        echo '<p>'._e( 'Die Posts entsprechen nicht den Kriterien.' ).'</p>';   
    }
    wp_reset_postdata();
    ?>
    

    【讨论】:

      【解决方案2】:

      使用默认的 WordPress 分页,这里是一个例子:

      <?php
      // set the "paged" parameter 
      $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
      $args = array(
          'posts_per_page' => 5,
          'paged' => $paged,
      );
      $the_query = new WP_Query( $args );
      if ( $the_query->have_posts() ) :
      // the loop
         while ( $the_query->have_posts() ) : $the_query->the_post(); 
            the_title();
         endwhile;
      
      // Pagination
      echo get_next_posts_link( 'Older', $the_query->max_num_pages );
      echo get_previous_posts_link( 'Newer' );
      
      // clean up after our query
      wp_reset_postdata(); 
      
      else:
      _e( 'Sorry, no posts matched your criteria.' );
      endif;
      

      【讨论】:

        【解决方案3】:

        要自动显示下一个和上一个链接,只需使用以下函数:

        next_posts_link();
        previous_posts_link();
        

        https://codex.wordpress.org/Pagination 可以帮助解决这个问题。在自定义循环中,它有助于将 $max_pages 变量作为零传递给无限页,所以它看起来像:

        next_posts_link("Older Posts", 0);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-30
          • 1970-01-01
          相关资源
          最近更新 更多