0

I'm trying to fix a theme which has the following setup: on each page the user can select to list posts instead of the page content. The problem is that the pagination doesn't work on this page even thou the same functions works fine on the main query (on homepage). When I say that the pagination doesn't work I mean that when I click on page 2 I get the same posts as on page 1.

This is the code for the 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(); ?>

Then this is the code for 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>

And finally this is the pagination function from 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";
        }
    }
}

As I said, everything works fine on homepage, the pagination breaks down only on the secondary pages (with the list posts option enable).

0

The solution:

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

The explanation: the previous code was missing the $paged variable, which should have been integrated into the query. Otherwise WP doesn't know which posts to show on which page.

-1
<?php get_header(); ?>
<?php query_posts(array( 
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ), )); ?>

I think this helps

  • Please explain how this helps... – kaiser Oct 14 '14 at 20:35
  • I see that solution found – Gazi Oct 14 '14 at 20:50
  • Ehm... wat? "I see that solution found" is a sentence that does not make any sense to me. Could you please rephrase that? – kaiser Oct 14 '14 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.