简体   繁体   English

WordPress Pagination与Page 1相同

[英]WordPress Pagination not working Page 2 same as Page 1

I'm trying to get WordPress Pagination Working. 我正在努力让WordPress分页工作。

I've used different plugins and tried tweaking the code in the pagination.php function to no avail. 我使用了不同的插件,并尝试调整pagination.php函数中的代码无济于事。

No matter what plugin or tweak I've used so far, Pages 2, 3 etc always displays the same set of posts. 无论到目前为止我使用过什么插件或调整,第2页,第3页等都会显示相同的帖子。

Here is the code in the pagination.php 这是pagination.php中的代码

<!-- Previous / More Entries -->        
<div class="mdnw_pagination">

<?php if(function_exists('wp_paginate')) :
    wp_paginate();
; else : ?>
<div class="p button"><?php next_posts_link(__('« Previous Posts', 'skeleton')); ?></div>
<div class="m button"><?php previous_posts_link(__('Next Posts »', 'skeleton')); ?></div>
<?php endif; ?>

</div>
<!-- </Previous / More Entries -->

Here is the code for the blog template for the home page: 以下是主页的博客模板的代码:

<!-- THE POST QUERY -->
                        <?php 

                    wp_reset_query();

                    global $paged;
                    global $template_file;
                    $cat_string = '';
                    $format = '';

                    if( get_post_custom_values('blog_post_count') ) :  
                        $post_array = get_post_custom_values('blog_post_count');
                        $post_count = join(',', $post_array);
                    else : 
                        $post_count = -1;
                    endif;

                    /* Get Category Filter */
                    if(get_custom_field('blog_category_filter' )) :
                        $cats = get_custom_field( 'blog_category_filter' );
                        foreach ( $cats as $cat ) {
                            $acats[] = $cat;                
                        }
                        $cat_string = join(',', $acats);                    
                    endif;

                    $args=array(
                        'cat'=>$cat_string,            // Query for the cat ID's (because you can't use multiple names or slugs... crazy WP!)
                        'posts_per_page'=>$post_count, // Set a posts per page limit
                        'paged'=>$paged,               // Basic pagination stuff.
                       );

                    query_posts($args); ?>

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

                        <?php get_template_part( 'includes/format', $format ); ?>

                    <?php endwhile; else: ?>

                        <div class="post">
                              <p><?php _e('Sorry, no posts matched your criteria.', 'skeleton') ?></p>
                        </div><!-- /.post -->

                    <?php endif; ?>  
                    <?php get_template_part( 'includes/element', 'pagination' ); ?>                    
                    <?php wp_reset_query(); ?>                  

                </div>

What should I change in either file to get it to show any other content but the first page? 我应该在两个文件中更改什么才能让它显示除第一页之外的任何其他内容?

I would change the reading pane settings but the query posts function uses a dynamic value that I'm unaware of. 我会更改阅读窗格设置,但查询帖子功能使用我不知道的动态值。

How or what can I change to get it working? 如何或我可以改变什么来使它工作?

I tried the solution on this page https://wordpress.stackexchange.com/questions/105977/wordpress-pagination-not-working-always-showing-first-pages-content , but to no avail: 我尝试了这个页面上的解决方案https://wordpress.stackexchange.com/questions/105977/wordpress-pagination-not-working-always-showing-first-pages-content ,但无济于事:

Here is what I changed the code to: 这是我将代码更改为:

<?php 

                    wp_reset_query();

                    global $paged;
                    global $template_file;
                    $cat_string = '';
                    $format = '';

                    if( get_post_custom_values('blog_post_count') ) :  
                        $post_array = get_post_custom_values('blog_post_count');
                        $post_count = join(',', $post_array);
                    else : 
                        $post_count = -1;
                    endif;

                    /* Get Category Filter */
                    if(get_custom_field('blog_category_filter' )) :
                        $cats = get_custom_field( 'blog_category_filter' );
                        foreach ( $cats as $cat ) {
                            $acats[] = $cat;                
                        }
                        $cat_string = join(',', $acats);                    
                    endif;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
                    $args=array(
                        'cat'=>$cat_string,            // Query for the cat ID's (because you can't use multiple names or slugs... crazy WP!)
                        'posts_per_page'=> 9, // Set a posts per page limit
                        'paged'=>$paged,               // Basic pagination stuff.
                       );

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

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

                        <?php get_template_part( 'includes/format', $format ); ?>

                    <?php endwhile; else: ?>

                        <div class="post">
                              <p><?php _e('Sorry, no posts matched your criteria.', 'skeleton') ?></p>
                        </div><!-- /.post -->

                    <?php endif; ?>  
                    <?php get_template_part( 'includes/element', 'pagination' ); ?>                    
                    <?php wp_reset_query(); ?>  

I fixed it. 我修好了它。

I found the fix here: http://themeforest.net/forums/thread/pagination-on-wordpress-static-page-set-to-front-page/28120 我在这里找到了解决方法: http//themeforest.net/forums/thread/pagination-on-wordpress-static-page-set-to-front-page/28120

Instead of doing this: 而不是这样做:

   $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts( array( 'post_type' => 'post', 'paged' => $paged ) );

I did this: 我这样做了:

if ( get_query_var('paged') ) {

$paged = get_query_var('paged');

} elseif ( get_query_var('page') ) {

$paged = get_query_var('page');

} else {

   $paged = 1;

}

query_posts( array( 'post_type' => 'post', 'paged' => $paged ) );

Now it works! 现在它有效!

If I have gaps between new themes I often forget about this pagination problem and have to spend an hour remembering how I corrected it for myself. 如果我在新主题之间存在差距,我经常会忘记这个分页问题,​​并且必须花一个小时记住我是如何为自己纠正的。

My method is to put all the query options in functions.php rather than on page. 我的方法是将所有查询选项放在functions.php而不是页面上。

Example, if using pagination, starting your index.php loop like this produces the page-2-is-same-as-page-1 bug. 例如,如果使用分页,则像这样启动index.php循环会产生page-2-is-same-as-page-1错误。

<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => 4, 'orderby' => 'date', 'order' => 'DESC', 'tag' => 'pics, vids' );
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>

so change that to just the default no-frills loop opener like this.. 所以把它改成这样的默认的简单循环开启器..

<?php while (have_posts()) : the_post(); ?>

and then put those same options in functions.php so it would look something like this.. 然后将相同的选项放在functions.php中,这样看起来就像这样。

function modify_query_order_index( $query ) {
   if ( $query->is_front_page() && $query->is_main_query() && !is_admin() ) {
      $query->set( 'orderby', 'date' );
      $query->set( 'order', 'DESC' );
      $query->set( 'posts_per_page', '4' );
      $query->set( 'post_type', 'post' );
      $query->set( 'tag', 'pics, vids' );
   }
}
add_action( 'pre_get_posts', 'modify_query_order_index' );

and now depending on how your pagination function is written your pages 1, 2 and 3+ should be behaving as expected. 现在,根据您的分页功能的编写方式,您的第1,2和3页应该按预期运行。

bonus 奖金

adjusting posts_per_page with this query in functions will override the setting in admin panel > /settings/reading/ Blog pages show at most xx whereas setting posts per page with an in page query doesn't override that setting. 在函数中使用此查询调整posts_per_page将覆盖管理面板中的设置> / settings / reading / 博客页面最多显示 xx,而使用页内查询设置每页的帖子不会覆盖该设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM