WordPress博客分页不起作用

时间:2020-10-18 作者:Pankaj Yadav

如果有人知道,请告诉我。。

     <?php
                $per_page = 6;
                $paged    = get_query_var(\'paged\') ? : 1;
                $offset   = (1 === $paged) ? 0 : (($paged - 1) * $per_page) + (($paged - 1) * 2);
                
                 $args = array(
                \'post_type\'=> \'post\',
                \'posts_per_page\' => $per_page,
                \'post_status\' => \'publish\',
                \'order\' => \'DESC\',
                \'orderby\' => \'ID\',
                \'paged\' => $paged,
                \'author__not_in\' => array(1),
                \'offset\' => $offset,
                  \'ignore_sticky_posts\' => 1
                
                );
                $result = new WP_Query( $args );
                    if ( $result-> have_posts() ) : ?>
                    <?php while ( $result->have_posts() ) : $result->the_post(); ?>
                          
                                    <div class="col-md-4 col-sm-12" style="float:left;">
                                        <div class="authorname">
                                         <?php //geting here post author name
                                            echo get_the_author(); 
                                            
                                          ?>
                                        </div>
                                               <h2 class="post_title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                                                <div class="featured_image">
                                                    <a href="<?php the_permalink() ?>">
                                                        <?php
                                                         the_post_thumbnail(\'full\');
                                                         ?>
                                                     </a>
                                                 </div>
                                                 
                                               <div class="excerpt">
                                                    <?php
                                                     $cats = get_the_category($id);
                                                    
                                                    // excerpt of post
                                                    the_excerpt();
                                                    
                                                    ?>  
                                                    
                                               </div>
                                               <div class="category_name">
                                                   <?php foreach ( $cats as $cat ): ?>
                                                        <a href="<?php echo get_category_link($cat->cat_ID); ?>">
                                                            <?php echo $cat->name; echo ", "; ?>
                                                        </a>
                                                    <?php endforeach; ?>
                                               </div>
                                    </div>
                    <?php endwhile; ?>
                                <!-- Pagination -->
                                <nav class="pagination">
<?php  previous_posts_link(\'&laquo; Previous\', $query->max_num_pages);
if ($paged > 1) echo \' | \';
next_posts_link(\'More &raquo;\', $query->max_num_pages);

echo \'<br> Showing \' . $offset . \'-\' . ($offset + 6) . \' of \' . $query->found_posts . \' posts.\';
 ?>
</nav>
                                <!-- pagination ends here -->
                    <?php endif; wp_reset_postdata();  ?>
                   </div> 
                   </div>
            
          <?php
          
          
          ?>

1 个回复
SO网友:Valerii Vasyliev

改变

$paged    = get_query_var(\'paged\') ? : 1;

$paged = absint(
    max(
        1,
        get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : get_query_var( \'page\' )
    )
);
更改

$query

$result
最终代码

 <?php
                $per_page = 6;

                $paged = absint(
                                           max(
                                                        1,
                                                          get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : get_query_var( \'page\' )
                                               )
                               );
                
                $offset   = (1 === $paged) ? 0 : (($paged - 1) * $per_page) + (($paged - 1) * 2);
                
                 $args = array(
                \'post_type\'=> \'post\',
                \'posts_per_page\' => $per_page,
                \'post_status\' => \'publish\',
                \'order\' => \'DESC\',
                \'orderby\' => \'ID\',
                \'paged\' => $paged,
                //\'author__not_in\' => array(1),
                \'offset\' => $offset,
                  \'ignore_sticky_posts\' => 1
                
                );
                $result = new WP_Query( $args );
                
                    if ( $result-> have_posts() ) : ?>
                    <?php while ( $result->have_posts() ) : $result->the_post(); ?>
                          
                                    <div class="col-md-4 col-sm-12" style="float:left;">
                                        <div class="authorname">
                                         <?php //geting here post author name
                                            echo get_the_author(); 
                                            
                                          ?>
                                        </div>
                                               <h2 class="post_title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                                                <div class="featured_image">
                                                    <a href="<?php the_permalink() ?>">
                                                        <?php
                                                         the_post_thumbnail(\'full\');
                                                         ?>
                                                     </a>
                                                 </div>
                                                 
                                               <div class="excerpt">
                                                    <?php
                                                     $cats = get_the_category($id);
                                                    
                                                    // excerpt of post
                                                    the_excerpt();
                                                    
                                                    ?>  
                                                    
                                               </div>
                                               <div class="category_name">
                                                   <?php foreach ( $cats as $cat ): ?>
                                                        <a href="<?php echo get_category_link($cat->cat_ID); ?>">
                                                            <?php echo $cat->name; echo ", "; ?>
                                                        </a>
                                                    <?php endforeach; ?>
                                               </div>
                                    </div>
                    <?php endwhile; ?>
                    
                                <!-- Pagination -->
                                <nav class="pagination">
<?php  previous_posts_link(\'&laquo; Previous\', $result->max_num_pages);
if ($paged > 1) echo \' | \';
next_posts_link(\'More &raquo;\', $result->max_num_pages);

echo \'<br> Showing \' . $offset . \'-\' . ($offset + 6) . \' of \' . $result->found_posts . \' posts.\';
 ?>
</nav>
                                <!-- pagination ends here -->
                    <?php endif; wp_reset_postdata();  ?>
                   </div> 
                   </div>

相关推荐

Custom pagination structure

当前我的分页工作方式如下http://example.com/city/usa/ #for page 1 http://example.com/city/usa/page/2 http://example.com/city/usa/page/3 ...and so on 我想将其更改为:http://example.com/city/usa/ #for page 1 http:/