Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a specific template that displays only posts from a category (350). When I paginate my posts, the posts stay the same. Below is some code that makes up this template. Any ideas how to correct this?

<?php if (is_front_page()) { ?>
    <?php get_template_part('home'); ?>
<?php } else { ?>   
    <?php 
    $et_ptemplate_settings = array();
    $et_ptemplate_settings = maybe_unserialize( get_post_meta($post->ID,'et_ptemplate_settings',true) );

    $fullwidth = isset( $et_ptemplate_settings['et_fullwidthpage'] ) ? (bool) $et_ptemplate_settings['et_fullwidthpage'] : false;

    $et_ptemplate_blogstyle = isset( $et_ptemplate_settings['et_ptemplate_blogstyle'] ) ? (bool) $et_ptemplate_settings['et_ptemplate_blogstyle'] : false;

    $et_ptemplate_showthumb = isset( $et_ptemplate_settings['et_ptemplate_showthumb'] ) ? (bool) $et_ptemplate_settings['et_ptemplate_showthumb'] : false;

    $blog_cats = isset( $et_ptemplate_settings['et_ptemplate_blogcats'] ) ? (array) $et_ptemplate_settings['et_ptemplate_blogcats'] : array();
    $et_ptemplate_blog_perpage = isset( $et_ptemplate_settings['et_ptemplate_blog_perpage'] ) ? (int) $et_ptemplate_settings['et_ptemplate_blog_perpage'] : 10;
    ?>

        <div id="content" class="clearfix<?php if($fullwidth) echo(' pagefull_width');?>">
            <div id="content-area"> 
                <div class="entry clearfix">
                <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                    <?php query_posts('cat=350'); ?>

                    <h1 class="title"><?php the_title(); ?></h1>

                    <?php the_content(); ?>
                    <?php wp_link_pages(array('before' => '<p><strong>'.esc_html__('Pages','Minimal').':</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>

                    <div id="et_pt_blog">
                        <?php $cat_query = ''; 
                        if ( !empty($blog_cats) ) $cat_query = '&cat=' . implode(",", $blog_cats);
                        else echo '<!-- blog category is not selected -->'; ?>
                        <?php 
                            $et_paged = is_front_page() ? get_query_var( 'page' ) : get_query_var( 'paged' );
                        ?>
                        <?php query_posts("showposts=$et_ptemplate_blog_perpage&paged=" . $et_paged . $cat_query); ?>
                        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                            <div class="et_pt_blogentry clearfix">
                                    <?php the_content(); ?>
                            </div> <!-- end .et_pt_blogentry -->

                        <?php endwhile; ?>
                            <div class="page-nav clearfix">
                                <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
                                else { ?>
                                     <?php get_template_part('includes/navigation'); ?>
                                <?php } ?>
                            </div> <!-- end .entry -->
                        <?php else : ?>
                            <?php get_template_part('includes/no-results'); ?>
                        <?php endif; wp_reset_query(); ?>

                    </div> <!-- end #et_pt_blog -->

                <?php endwhile; endif; ?>
                </div> <!-- end .entry -->

            </div> <!-- end #content-area -->   

    <?php if (!$fullwidth) get_sidebar(); ?>
        </div> <!-- end #content --> 

    <?php get_footer(); ?>
<?php } ?>  
share|improve this question
add comment

1 Answer 1

The problem is that you are running query_posts in the middle of the page, which, by the way you should nearly never do. That will overwrite the main query-- the global $wp_query object. Pagination should probably appear to work. The problem is that the original main query runs before your template loads and thus before the modified version of your query runs, and because of that the main query and the one that is being paginated are out of sync.

You probably need to use a filter on pre_get_posts, something like this one, instead of running another query in the middle of the page. In your case you are going to need to pull in some information.

Here is a best guess at what you need (completely untested):

function pre_get_posts_wpse_104225($qry) {
  if (is_main_query() && !is_front_page()) {
    $et_ptemplate_blog_perpage = isset( $et_ptemplate_settings['et_ptemplate_blog_perpage'] ) 
      ? (int) $et_ptemplate_settings['et_ptemplate_blog_perpage'] 
      : 10;

    $blog_cats = isset( $et_ptemplate_settings['et_ptemplate_blogcats'] ) 
      ? (array) $et_ptemplate_settings['et_ptemplate_blogcats'] 
      : array();

    $cat_query = implode(",", $blog_cats);

    $qry->set('posts_per_page',$et_ptemplate_blog_perpage);
    $qry->set('category__in', $cat_query);
  }
}
add_action('pre_get_posts','pre_get_posts_wpse_104225');

I am sure you would need more conditions that this-- if (is_main_query() && !is_front_page()) {-- but I can't tell exactly what else.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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