WordPress Pagination not working Page 2 same as Page 1

半世苍凉 提交于 2020-12-26 07:22:17

问题


I'm trying to get WordPress Pagination Working.

I've used different plugins and tried tweaking the code in the pagination.php function to no avail.

No matter what plugin or tweak I've used so far, Pages 2, 3 etc always displays the same set of posts.

Here is the code in the 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:

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(); ?>  

回答1:


I fixed it.

I found the fix here: 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!




回答2:


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.

Example, if using pagination, starting your index.php loop like this produces the page-2-is-same-as-page-1 bug.

<?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..

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.

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.



来源:https://stackoverflow.com/questions/21345744/wordpress-pagination-not-working-page-2-same-as-page-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!