个性化阅读
专注于IT技术分析

WordPress分页不起作用与第1页相同的第2页

点击下载

我正在尝试使WordPress分页正常工作。

我使用了不同的插件, 并尝试调整pagination.php函数中的代码无济于事。

不管我到目前为止使用了什么插件或调整, Pages 2, Page 3等总是显示相同的帖子集。

这是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 -->

这是主页博客模板的代码:

<!-- 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>

我应该在这两个文件中进行哪些更改才能显示除首页以外的其他内容?

我会更改阅读窗格的设置, 但查询帖子功能使用的是我不知道的动态值。

我如何或如何更改才能使其正常工作?

我在此页面上尝试了解决方案https://wordpress.stackexchange.com/questions/105977/wordpress-pagination-not-working-always-showing-first-pages-content, 但无济于事:

这是我将代码更改为的内容:

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


我修好了它。

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

而不是这样做:

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

我这样做:

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

现在可以了!


#2


如果我在新主题之间有分歧, 我通常会忘记这个分页问题, ​​而不得不花一个小时记住我自己如何纠正它。

我的方法是将所有查询选项放在functions.php中, 而不是放在页面上。

例如, 如果使用分页, 则像这样启动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();
?>

因此将其更改为仅这样的默认无装饰循环打开器。

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

然后将这些相同的选项放到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' );

现在, 根据分页功能的编写方式, 第1、2和3+页的行为应与预期一致。

奖金

在函数中使用此查询来调整posts_per_page将会覆盖管理面板> / settings / reading /博客页面最多显示xx的设置, 而使用页面内查询设置每页的帖子数不会覆盖该设置。

赞(0)
未经允许不得转载:srcmini » WordPress分页不起作用与第1页相同的第2页

评论 抢沙发

评论前必须登录!