当前位置: 首页 > 知识库问答 >
问题:

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

娄浩荡
2023-03-14

为了学习wordpress的开发,我正在从头开始构建一个wordpress主题。现在我想在我的类别页面上添加分页,但问题是:当我单击older-post-link时,url从“http://localhost/WordPress4/category/Bloc1/page/2/”变为“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中,分页也会在category页面上工作。但是第二个类别页面(“http://localhost/wordpress4/category/bloc1/page/2/”)将采用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');

最后,这是我的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(); ?>

谢谢.

共有1个答案

江俊能
2023-03-14

使用这个代码,也许它会解决你的问题

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

 类似资料:
  • 我正在创建我自己的WordPress主题,我已经用MAMP在本地设置了它。一切正常,当我在浏览器中输入localhost/wordpress时,主题就会出现。但是当我进入localhost/wordpress/wp-content/themes/mysitename它只是一个白色的空白页。 在主题下的WordPress仪表板中,我的主题旁边的图像(它允许您激活它)也是空白的。 我怎样才能让它出现?

  • 这件事让我毛骨悚然。我正在使用CodeIgniters分页库,现在它总是作为当前页面粘贴在第1页上。我已经检查了一大堆StackOverflow问题,我没有和其他人一样的问题。 这是我的url结构 这是我控制器里的分页代码 当我在视图中呼应分页时,它看起来像是工作的。每个链接上的网址都是正确的,一切看起来都很好。最后一个链接显示最后一个页面url,当前页面为1。然而,当我点击第2页或分页中的任何其

  • 你恰好发现了 Keras。 Keras 是一个用 Python 编写的高级神经网络 API,它能够以 TensorFlow, CNTK 或者 Theano 作为后端运行。Keras 的开发重点是支持快速的实验。能够以最小的时延把你的想法转换为实验结果,是做好研究的关键。 如果你在以下情况下需要深度学习库,请使用 Keras: 允许简单而快速的原型设计(由于用户友好,高度模块化,可扩展性)。 同时支

  • 在我的Wordpress自定义主题中,一个分页,在加载上面1的页面时给出404错误。我已经检查了Permalink和其他东西,但没有scuccess。 请帮我脱掉。 这是我的密码。

  • 我有一个问题,在http://poshpanel.com/的主页上,选择后的特色图像被裁剪成600x250,并被放在主屏幕上,如下面的帖子所示。 我想让它变得更大,可以做些什么使它更大,更引人注目。 照片裁剪URLhttp://poshpanel.com/wp-content/uploads/2012/08/Pamela-Skaist-Levy-and-Gela-Nash-Taylor-1-600