wordpress分页返回第2页的空白页面(从头开始构建主题)

时间:2017-01-29 22:07:47

标签: php wordpress twitter-bootstrap pagination wordpress-theming

要学习wordpress开发,我从头开始构建wordpress主题。 现在我想在我的类别页面上添加分页,但问题是:  当我点击较旧的后链接时,网址会从" http://localhost/wordpress4/category/bloc1/"到" http://localhost/wordpress4/category/bloc1/page/2/"但它会把我带到一个空白页而不是显示其他帖子。

这是category.php

上的代码
    <?php get_header(); ?>

     <div class="container">
  <?php
  $counter = 1; //start counter

  $grids = 3; //Grids per row

  global $query_string; //Need this to make pagination work


  /*Setting up our custom query (In here we are setting it to show 12 posts per page and eliminate all sticky posts) */
  query_posts($query_string . '&caller_get_posts=1&posts_per_page=4');


  if(have_posts()) :   while(have_posts()) :  the_post(); 
  ?>
  <?php
  //Show the left hand side column
  if($counter == 1) :
  ?>
  <div class="row">
           <div class="col-md-4">
             <div class="center">
              <div class="postimage">
                 <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_custom_logo(); ?></a>
              </div>
                  <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                  <h4><?php the_category(); ?></h4>
           </div>
           </div>
  <?php

  elseif($counter == 2) :
  ?>
  <div class="col-md-4 border2">
     <div class="center">
              <div class="postimage">
                 <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_custom_logo(); ?></a>
              </div>
                  <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                  <h4><?php the_category(); ?></h4>
           </div>
           </div>

  <?php
  elseif($counter == $grids) :
  ?>
  <div class="col-md-4">
     <div class="center">
              <div class="postimage">
                 <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_custom_logo(); ?></a>
              </div>
                  <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                  <h4><?php the_category(); ?></h4>
           </div>
           </div>
  </div>
  <div class="clear"></div>
  <?php
  $counter = 0;
  endif;
  $counter++;
  endwhile;
  ?>


      <div class="row">
      <div class="col-xs-6 text-left">
         <?php next_posts_link('<< Older post'); ?>
      </div>
      <div class="col-xs-6 text-right">
         <?php previous_posts_link('Newer post >>'); ?>
      </div>


  <?php
  endif;
  ?>

  </div>
  </div>

  <?php get_footer(); ?>

我注意到如果我将以下代码添加到我的index.php中,分页工作也会在类别页面上进行。  但是第二个类别页面(&#34; http://localhost/wordpress4/category/bloc1/page/2/&#34;)将采用index.php的标记,因此帖子不会像第一个类别页面那样采用网格格式。

global $query_string; //Need this to make pagination work


  /*Setting up our custom query (In here we are setting it to show 12 posts per page and eliminate all sticky posts) */
  query_posts($query_string . '&caller_get_posts=1&posts_per_page=4');

同样在类别页面上,旧的后链接显示在行之间而不是显示在页面的底部。 enter image description here

最后这是我的index.php上的代码

<?php get_header(); ?>
<div class="container">
   <div class="row">
      <div class="col-xs-12 col-sm-8">  
         <?php 



         if(have_posts()):
                while(have_posts()): the_post(); ?>



                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">

                        <h3><?php the_title(); ?></h3>
                     <small><?php the_category(); ?></small>

                    </a>            

                    <p><?php the_content(); ?></p>
                    <hr/>


            <?php   endwhile;
            endif;

          ?>
      </div>

      <div class="col-xs-12 col-sm-4">
      <?php get_sidebar(); ?>
      </div>

   </div>
</div>



<?php get_footer(); ?>

谢谢。

2 个答案:

答案 0 :(得分:1)

使用此代码,可能会解决您的问题

<?php 
// the query to set the posts per page to 3
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 3, 'paged' => $paged );
query_posts($args); ?>
<!-- the loop -->
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
        <!-- rest of the loop -->
        <!-- the title, the content etc.. -->
<?php endwhile; ?>
<!-- pagination -->

<div class="row">
  <div class="col-xs-12>"
    <div class="col-xs-6 text-left"><?php next_posts_link(); ?></div>
    <div class="col-xs-6 text-right"><?php previous_posts_link(); ?></div>
  </div>
</div>
<?php else : ?>
<!-- No posts found -->
<?php endif; wp_reset_postdata(); ?>

有关详细信息,请查看此链接https://codex.wordpress.org/Pagination

答案 1 :(得分:0)

经过大量搜索,我找到了解决方案。 问题是The&#34;博客页面最多显示&#34;在wordpress阅读设置中干扰了&#34; posts_per_page = 4&#34;我通过query_posts()声明。 解决方案:

我删除了&#34; query_posts()&#34;因为最好使用WP_Query()或pre_get_posts过滤器。 对我来说即使使用wp_query我也无法让分页工作,所以我尝试使用pre_get_posts过滤器并且它有效。 所以在category.php中我删除了query_posts并只使用了普通循环。

这是我在category.php中的新代码

<?php get_header(); ?>

     <div class="container">
  <?php
  $counter = 1; //start counter

  $grids = 3; //Grids per row



  if(have_posts()) :  
     while(have_posts()) :  the_post(); 


  ?>
  <?php
  //Show the left hand side column
  if($counter == 1) :
  ?>
  <div class="row">
           <div class="col-md-4">
             <div class="center">
              <div class="postimage">
                 <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_custom_logo(); ?></a>
              </div>
                  <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                  <h4><?php the_category(' '); ?></h4>
           </div>
           </div>
  <?php

  elseif($counter == 2) :
  ?>
  <div class="col-md-4 border2">
     <div class="center">
              <div class="postimage">
                 <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_custom_logo(); ?></a>
              </div>
                  <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                  <h4><?php the_category(' '); ?></h4>
           </div>
           </div>

  <?php
  elseif($counter == $grids) :
  ?>
  <div class="col-md-4">
     <div class="center">
              <div class="postimage">
                 <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_custom_logo(); ?></a>
              </div>
                  <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                  <h4><?php the_category(' '); ?></h4>
           </div>
           </div>
  </div>
  <div class="clear"></div>
  <?php
  $counter = 0;
  endif;
  ?>

  <?php
  $counter++;
  endwhile;
   ?>

              <div class="row">

                  <div class="col-xs-12 text-left ">
                     <?php next_posts_link('<< Older post'); ?>
                  </div>
                  <div class="col-xs-12 text-right ">
                     <?php previous_posts_link('Newer post >>'); ?>
                  </div>

              </div>



  <?php
  endif;
  ?>
     </div>

  <?php get_footer(); ?>

然后我在function.php上添加了pre_get_posts动作 这是代码:

add_action( 'pre_get_posts', function ( $q )
{
    if (        !is_admin() // Very important, otherwise back end queries will be affected as well
          && $q->is_main_query() // Very important, we just need to modify the main query
          && $q->is_category() // Only target category pages
    ) {

                  $q->set( 'posts_per_page', 2 );


    }
});

我希望我的回答能帮助那些遇到同样问题的人,即使我的回答没有得到很好的解释。 了解更多信息,请搜索以下内容: 使用wp_query分页 使用pre_get_posts为自定义页面设置分页。

如果开发人员可以更详细地解释我的解决方案并向我们提供有关使用pre_get_posts为自定义页面设置分页的更多信息,那将是非常好的。

相关问题