WP Pagination Loop无法在自定义主题中工作

时间:2017-06-08 03:04:22

标签: php wordpress

分页循环在自定义主题主页模板中不起作用。在所有页面中显示相同的内容。

您还可以向我推荐一些其他代码来解决此问题。

<?php 
// clear any other queries that may be in use!
wp_reset_query();
// check for $_GET paged value 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// setup post arguments
$args = array( 'posts_per_page' => 7, 'paged' => $paged, );
// run our query
query_posts($args);
// start loop
if (have_posts()) : while (have_posts()) : the_post(); 
  // if you use the <!-- more --> in your posts.
  global $more;
  $more = 0;
?>
   <div class="post">
     <?php the_title(); ?>
   </div>
<?php endwhile; ?>
<div class="navigation">
   <?php next_posts_link('Next'); ?>
   <?php previous_posts_link('Previous'); ?>
</div>
<?php else: ?>
   <div><h2>Nothing found</h2><p>No posts found for that query</p></div>
<?php endif; ?>

3 个答案:

答案 0 :(得分:2)

如果以上答案对您不起作用,请使用WP_Query而不是query_posts。

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
  'posts_per_page' => 3,
  'paged'          => $paged
);

$the_query = new WP_Query( $args ); 
?>

如果在静态首页上破坏了分页,则必须以这种方式添加“分页”参数:

if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
else { $paged = 1; }

答案 1 :(得分:1)

query_posts()不应由插件或主题使用。请改用WP_Query。它接受与query_posts相同的参数。请注意,这些方法都不是更改默认查询的最有效方法。事实上,任何一种方法都可以负责打破分页。

如果您的主题使用这些方法中的任何一种来查询主循环,则可以使用首选方法替换它,也就是说,挂钩到'pre_get_posts'并使用is_main_query()更改主查询。这种方式更快,更可靠,因为在从数据库中检索帖子之前,主循环的查询已被更改。

例如,假设您的主题在您的主页上查询主循环并且分页不起作用:

<?php 
// clear any other queries that may be in use!
wp_reset_query();
// check for $_GET paged value 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// setup post arguments
$args = array( 'posts_per_page' => 7, 'paged' => $paged, );
// run our query
query_posts($args);
// start loop
if (have_posts()) : while (have_posts()) : the_post(); 
  // if you use the <!-- more --> in your posts.
  global $more;
  $more = 0;
?>
   <div class="post">
     <?php the_title(); ?>
   </div>
<?php endwhile; ?>
<div class="navigation">
   <?php next_posts_link('Next'); ?>
   <?php previous_posts_link('Previous'); ?>
</div>
<?php else: ?>
   <div><h2>Nothing found</h2><p>No posts found for that query</p></div>
<?php endif; ?>

从代码中删除query_posts部分:

<?php 
// clear any other queries that may be in use!
wp_reset_query();
// check for $_GET paged value 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// setup post arguments
$args = array( 'posts_per_page' => 7, 'paged' => $paged, );
// run our query
query_posts($args);
?>

然后在主题的functions.php文件中添加主页的查询:

function my_post_queries( $query ) {
  // do not alter the query on wp-admin pages and only alter it if it's the main query
  if (!is_admin() && $query->is_main_query()){

    // alter the query for the home and category pages 

    if(is_home()){
      $query->set('posts_per_page', 7);
    }

    if(is_category()){
      $query->set('posts_per_page', 3);
    }

  }
}
add_action( 'pre_get_posts', 'my_post_queries' );

您可以使用条件标记来定位我们要更改查询的页面。像,

// alter the query for the Movies category page 
if(is_home() || is_front_page){
  $query->set('posts_per_page', 7);
}

答案 2 :(得分:0)

<?php
// Get current page and append to custom query parameters array
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
   // Define custom query parameters
   $args = array(  'posts_per_page' => 5, 'paged' => $paged );
    $custom_query_args = array( $args );

  // Instantiate custom query
  $custom_query = new WP_Query( $custom_query_args );

  // Pagination fix
  $temp_query = $wp_query;
  $wp_query   = NULL;
  $wp_query   = $custom_query;

// Output custom query loop
if ( $custom_query->have_posts() ) :
    while ( $custom_query->have_posts() ) :
        $custom_query->the_post();
                 ?>
             <div class="blog-post">
                 <?php the_post_thumbnail('post-thumbnail'); ?>

   <?php  endwhile;
endif;
// Reset postdata
wp_reset_postdata();

echo "<div class='blog-nav'>";

// Custom query loop pagination
previous_posts_link( 'Older Posts' );
next_posts_link( 'Newer Posts', $custom_query->max_num_pages );

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
  'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
  'format' => '?paged=%#%',
  'current' => max( 1, get_query_var('paged') ),
  'total' => $custom_query->max_num_pages
) );

// Reset main query object
echo "</div>";
$wp_query = NULL;
$wp_query = $temp_query;
?>