2010-07-14 176 views
0

我在WordPress页面上有以下代码。它基本上只抓取3个帖子,并显示它们以及页面内容本身。我想添加的是分页,以便用户可以浏览所有帖子,我如何使用这样的自定义循环来处理这个问题?页面上的WordPress分页

<?PHP 

     get_header(); 

     /* Template Name: News */ 

    ?> 

    <div style="padding: 0 20px;"> 


     <div class="box clearfix side" style="margin:10px 0;"> 

     <div style="float:left;width:628px;"> 

     <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
       <div class="content" id="post-<?php the_ID(); ?>"> 
        <h2><?php the_title(); ?><?php edit_post_link('Edit', ' <small>[', ']</small>'); ?></h2> 

     <?php the_content('<p>Read the rest of this page &raquo;</p>'); ?> 


        <?php wp_link_pages(array('before' => '<p>Pages: ', 'after' => '</p>', 'next_or_number' => 'number')); ?> 


        <hr /> 

       </div> 
       <?php endwhile; endif; ?> 

<hr /> 

     <?php $blog_query = new WP_Query('posts_per_page=3'); while ($blog_query->have_posts()) : $blog_query->the_post(); ?> 

      <div class="content" id="post-<?php the_ID(); ?>"> 

       <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4> 



       <?php the_excerpt(); ?> 

      </div> 



     <?php endwhile; ?> 

     <?php if ($blog_query->have_posts()) : ?> 

      <?php if(function_exists('wp_pagenavi')) 
      { 
       wp_pagenavi(); 
      } 
      ?> 

       <?php else: ?> 

       <h2>oooops!!!</h2> 


       <?php endif; ?> 


     </div> 


     </div> 

    </div> 

    <?PHP 

     get_footer(); 

    ?> 
+0

任何更新?尽快为客户项目提供这个服务。谢谢 – Cameron 2010-07-14 11:09:25

回答

0

原来你必须做这样的事情:

<?php $temp = $wp_query; $wp_query= null; ?>   

     <?php $wp_query = new WP_Query(array('posts_per_page' => 3, 'paged' => $paged)); while ($wp_query->have_posts()) : $wp_query->the_post(); ?> 

      <div class="content" id="post-<?php the_ID(); ?>"> 

       <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4> 

       <?php the_excerpt(); ?> 

      </div> 

     <?php endwhile; ?> 

     <?php if(function_exists('wp_pagenavi')) 
     { 
      wp_pagenavi(); 
     } 
     ?> 

     <?php $wp_query = null; $wp_query = $temp; ?> 
+0

是的,所以基本上你必须声明全局wp_query并将其设置在临时va中。很尴尬:/ – Cameron 2010-07-14 22:01:18

0

你确定你没有重新发明轮子吗?为什么不设置要显示在管理员的帖子数量,然后使用WP的本地分页为博客?

+0

如果我在管理员或代码中设置号码并使用本地分页,则无关紧要。问题在于它没有显示出来,或者当我手动输入/ page/2 /时,它没有显示下一页的帖子。所以有些事情要做,但是什么? – Cameron 2010-07-14 18:41:59

+0

这是什么显示? 404?这是一个页面模板还是标准的'index.php'?分页只能在'index.php'里面的标准'have_posts()'循环中工作 – TheDeadMedic 2010-07-14 20:23:57

+0

它只是显示相同的内容,所以本质上它忽略了url,但它也没有显示分页! – Cameron 2010-07-14 21:31:40