二次查询中分页不起作用

时间:2014-10-14 作者:George Grigorita

我正在尝试修复一个具有以下设置的主题:在每个页面上,用户可以选择列出帖子,而不是页面内容。问题是分页在这个页面上不起作用,即使相同的功能在主查询(主页)上也可以正常工作。当我说分页不起作用时,我的意思是当我点击第2页时,我得到的帖子和第1页上的一样。

这是page.php:

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

    $display_posts = get_post_meta($post->ID,\'zephyr_display_posts\', true);
    $zephyr_full_content = get_post_meta($post->ID,\'zephyr_full_content\', true);
    $zephyr_post_columns = get_post_meta($post->ID,\'zephyr_meta_box_columns\', true);
    if ( $zephyr_post_columns == \'\' ) { $zephyr_post_columns = \'col-md-6\'; }
    if ( $display_posts == \'on\' ) {
        if ( $posts_slider == \'on\' ) { get_template_part( \'templates/post\', \'slider\'); }
        if ( $zephyr_page_layout == \'no-sidebar\' ) {
            get_template_part(\'templates/content\', \'nosidebar\');
        } else {
            get_template_part(\'templates/content\', \'\');
        }
    } else {
        if ( $zephyr_page_layout == \'no-sidebar\' ) {
            get_template_part(\'templates/content\', \'page-nosidebar\');
        } else {
            get_template_part(\'templates/content\', \'page\');
        }
    } ?>
    <?php endwhile; else : ?>
        <div class="col-md-offset-2 col-xs-offset-2 col-sm-offset-2 col-md-10 col-xs-10 col-sm-10">
        <h1><?php _e(\'No matching posts were found\', \'zephyr\'); ?></h1>
        </div>
    <?php endif; ?>
    <?php if ( $zephyr_page_layout !== \'no-sidebar\' ) { get_template_part(\'templates/sidelist\', \'\'); } ?>
<?php get_footer(); ?>
那么这就是content.php:

    <?php // building query
    $zephyr_cat = get_post_meta($post->ID, \'zephyr_category\', true);
    if ( $zephyr_cat == 0 ) { 
        $zephyr_q = new WP_query(\'posts_per_page=\'.get_option(\'posts_per_page\'));
    } else {
        $zephyr_q = new WP_query(\'cat=\'.$zephyr_cat);
    }
    global $zephyr_i;
    $pages = $zephyr_q->max_num_pages;
    $zephyr_i = 0;
    if ( $zephyr_q->have_posts() ) : while ( $zephyr_q->have_posts() ) : $zephyr_q->the_post(); ?>
        <?php 
        if ( $zephyr_post_layout == \'post-masonry\' ) {
            $zephyr_i++;
            get_template_part( \'templates/post\', \'masonry\'.get_post_format() );
        } else {
            get_template_part( \'templates/post\', get_post_format() );
        }
        zephyr_get_ads_post($zephyr_q);
    endwhile; else : ?>
        <h2><?php _e(\'No posts found, go \', \'zephyr\'); ?> <a href="<?php echo home_url(); ?>" title="<?php bloginfo(\'name\'); ?>"><?php _e(\'home\', \'zephyr\'); ?></a>?</h2>
    <?php endif; ?>
    <div class="clearfix"></div>
    <?php zephyr_pagination($pages) ?>
</div>
最后,这是来自的分页函数functions.php:

function zephyr_pagination($pages = \'\', $range = 2) {
    if ( !get_theme_mod(\'zephyr_infinite\') ) {
        $showitems = ($range * 2)+1;
        global $paged;
        if(empty($paged)) $paged = 1;
        if($pages == \'\') {
            global $wp_query;
            $pages = $wp_query->max_num_pages;
            if(!$pages) {
                $pages = 1;
            }
        }
        if(1 != $pages) {
            echo "<div class=\'row paginationrow\'>";
            echo "<div class=\'zephyr-pagination col-md-10 col-md-offset-2\'>";
            if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a class=\'first\' href=\'".get_pagenum_link(1)."\'>".__(\'First\', \'zephyr\')."</a>";
            if($paged > 1 && $showitems < $pages) echo get_previous_posts_link(__(\'Prev\', \'zephyr\'));
            for ($i=1; $i <= $pages; $i++) {
                if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
                    echo ($paged == $i)? "<span class=\'current\'>".$i."</span>":"<a href=\'".get_pagenum_link($i)."\' class=\'inactive\' >".$i."</a>";
                }
            }
            if ($paged < $pages && $showitems < $pages) echo get_next_posts_link(__(\'Next\', \'zephyr\'));
            if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href=\'".get_pagenum_link($pages)."\'>".__(\'Last\', \'zephyr\')."</a>";
            echo "</div>\\n";
            echo "</div>\\n";
        }
    }
}
正如我所说的,主页上的一切都很好,分页只在辅助页面上出现(启用列表帖子选项)。

2 个回复
SO网友:George Grigorita

解决方案:

<?php // building query
    $zephyr_cat = get_post_meta($post->ID, \'zephyr_category\', true);
    $paged = is_front_page() ? get_query_var( \'page\' ) : get_query_var( \'paged\' );
    if ( $zephyr_cat == 0 ) { 
        $zephyr_q = new WP_query(\'posts_per_page=\'.get_option(\'posts_per_page\') . \'&paged=\' . $paged);
    } else {
        $zephyr_q = new WP_query(\'cat=\'.$zephyr_cat . \'&paged=\' . $paged); }
说明:前面的代码缺少$paged 变量,该变量应已集成到查询中。否则,WP不知道在哪个页面上显示哪些帖子。

SO网友:Gazi
<?php get_header(); ?>
<?php query_posts(array( 
\'paged\' => ( get_query_var(\'paged\') ? get_query_var(\'paged\') : 1 ), )); ?>

I think this helps

结束

相关推荐

Pagination Custom post type

我有一个名为“doel”的自定义帖子类型。我制作了一个页面模板来显示所有doel项目在这个页面模板中,我循环使用“doel”post类型,在其中6个之后,我想进行分页。仅显示分页。我开始上网,这似乎是wp 3.4版之后的常见问题我在这个网站和wp论坛上尝试了一些解决方案,但没有一个有效。例如,我尝试的是调整循环,因此分页将在循环中、循环外或endwhile和endif之间。此外,我还尝试了wp pagenavi插件和最初的wp previous和next post链接,但它们都不起作用,所以我认为我的循环