博客登录页的分页

时间:2013-06-28 作者:Scott Simpson

我在mysite有一个博客页面。com/blog。在这一页上,我有一个循环:

<?php

$args = array (
  \'pagination\'             => true,
  \'paged\'                  => \'pages\',
  \'posts_per_page\'         => \'5\',
  \'ignore_sticky_posts\'    => true
 );
?>

<?php $query = new WP_Query( $args ); ?>

<?php if($query->have_posts()) : while($query->have_posts()) : $query->the_post(); ?>

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

<?php the_title(\'<h2 class="entry-title"><a href="\' . get_permalink() . \'" title="\' . the_title_attribute(\'echo=0\') . \'" rel="bookmark">\', \'</a></h2>\'); ?>

<p class="byline">
  <span class="author vcard"><?php the_author_posts_link(); ?></span> <span class="sep">|</span> 
  <abbr class="published" title="<?php the_time(__(\'l, F jS, Y, g:i a\', \'example\')); ?>"><?php the_time(__(\'F j, Y\', \'example\')); ?></abbr>
  <?php edit_post_link(__(\'Edit\', \'example\'), \' <span class="sep">|</span> <span class="edit">\', \'</span> \'); ?>
</p>

<div class="entry-content">
  <?php the_content(); ?>
  <?php wp_link_pages(\'before=<p class="pages">\' . __(\'Pages:\',\'example\') . \'&after=</p>\'); ?>
</div>

<p class="entry-meta">
  <span class="categories"><?php _e(\'Posted in\', \'example\'); ?> <?php the_category(\', \'); ?></span>
  <?php the_tags(\'<span class="tags"> <span class="sep">|</span> \' . __(\'Tagged\', \'example\') . \' \', \', \', \'</span>\'); ?> 
  <span class="sep">|</span> <?php comments_popup_link(__(\'Leave a response\', \'example\'), __(\'1 Response\', \'example\'), __(\'% Responses\', \'example\'), \'comments-link\', __(\'Comments closed\', \'example\')); ?> 
</p>

这就像预期的那样,但是我无法让分页工作。我尝试了多种选择。非常感谢您的帮助。

2 个回复
最合适的回答,由SO网友:eteich 整理而成

这对我有用,没有404

 <?php
 $args = array (
    \'posts_per_page\' => 5 ,
    \'paged\' => $paged,
    \'ignore_sticky_posts\'    => true
  );
  $blog_query = new WP_Query($args);
  if ( $blog_query->max_num_pages > 1 ) { 
     next_posts_link(\'&larr; Older posts\', $blog_query->max_num_pages); 
     previous_posts_link(\'Newer posts &rarr;\', $blog_query->max_num_pages); 
  } 
  while ( $blog_query->have_posts() ) {
      $blog_query->the_post();

     // STUFF
  }
     if ( $blog_query->max_num_pages > 1 ) { 
     next_posts_link(\'&larr; Older posts\', $blog_query->max_num_pages); 
     previous_posts_link(\'Newer posts &rarr;\', $blog_query->max_num_pages); 
  }
  wp_reset_query();
  ?>
如果您不想使用类别页面,或者希望页面上有可编辑的内容,或者标题与类别名称不同,那么我可以看到需要使用二次循环。

SO网友:s_ha_dum

您不需要创建辅助循环。除了给服务器带来额外的工作之外ordinary pagination functions do not work well with secondary loops.

使用pre_get_posts 更改主查询。

function pregp_wpse_104648($qry) {
  if (is_main_query() && $qry->is_home()) {
    $qry->set(\'posts_per_page\',\'5\');
    $qry->set(\'ignore_sticky_posts\',true);
  }
}
add_action(\'pre_get_posts\',\'pregp_wpse_104648\');
我相信这就是你所需要的。

注:我在猜测您需要的条件。if (is_main_query() && $qry->is_home()) { 可能是错的,但这就是想法。

结束

相关推荐

Wordpress Pagination Problem

我想在wordpress索引中设置分页。php,但它不工作。在我的索引中。php有3个显示自定义帖子的循环部分,我想在索引中的Blog/recent post部分循环中设置分页。php。这是索引中的代码。php<?php /** * @package WordPress * @subpackage Adapt Theme */ $options = get_option( \'adapt_theme_settings\' ); ?> &l