0

This loop (used on a taxonomy-term archive page) is not paginating correctly. The older/newer links for page 2 are there, but when I click on older, it returns "no content found" (when what I expected was to see the second post of this archive). Have I missed something?

    <?php
/**
 * Template for various archive pages (category, tag, term, date, etc.
 */
get_header();
?>

<div id="content" class="stories span8" role="main">

        <?php if ( have_posts() ) { ?>

        <?php

            // queue up the first post so we know what type of archive page we're dealing with
            the_post();

            /*
             * Display some different stuff in the header
             * depending on what type of archive page we're looking at
             */

            // if it's an author page, show the author box with their bio, social links, etc.

            if ( is_author() ) {
                get_template_part( 'largo-author-box' );

            // for categories, tags, and custom taxonomies we show the term name and description

            } elseif ( is_category() || is_tag() || is_tax() ) {
                if ( is_category() ) {
                    $title = single_cat_title( '', false );
                    $description = category_description();
                } elseif ( is_tag() ) {
                    $title = single_tag_title( '', false );
                    $description = tag_description();
                } elseif ( is_tax() ) {
                    $title = single_term_title( '', false );
                    $description = term_description();
                }
        ?>
            <header class="archive-background clearfix">
                <?php

                    if ( $title)
                        echo '<h1><img src="http://largoproject.staging.wpengine.com/wp-content/themes/maine/images/whoyakidding_logo.jpg" border=0></h1>';

                        // category pages show a list of related terms

                    if ( is_category() && largo_get_related_topics_for_category( get_queried_object() ) != '<ul></ul>' ) { ?>
                        <div class="related-topics">
                            <h5><?php _e('Related Topics:', 'largo'); ?> </h5>
                            <?php echo largo_get_related_topics_for_category( get_queried_object() ); ?>
                        </div> <!-- /.related-topics -->
                <?php
                    }
                ?>

        <?php

            // if it's a date archive we'll show the date dropdown in lieu of a description

            } elseif ( is_date() ) {
        ?>
                    <nav class="archive-dropdown">
                        <select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'><option value=""><?php _e('Select Month', 'largo'); ?></option>
                        <?php wp_get_archives( array('type' => 'monthly', 'format' => 'option' ) ); ?>
                        </select>
                    </nav>
        <?php } ?>
            </header>

            <h3 class="recent-posts clearfix">
                <?php

                    /*
                     * Show a label for the list of recent posts
                     * again, tailored to the type of page we're looking at
                     */
                    $posts_term = of_get_option( 'posts_term_plural', 'Stories' );

                    if ( is_author() ) {
                        if ( function_exists( 'get_coauthors' ) && $author = get_coauthors( $post->ID ) ) {
                            printf(__('Recent %1$s<a class="rss-link" href="/author/%2$s/feed/"><i class="icon-rss"></i></a>', 'largo'), $posts_term, $author[0]->user_login );
                        } else {
                            printf(__('Recent %1$s<a class="rss-link" href="%2$s"><i class="icon-rss"></i></a>', 'largo'), $posts_term, get_author_feed_link( get_the_author_meta('ID') ) );
                        }
                    } elseif ( is_category() ) {
                        printf(__('Recent %1$s<a class="rss-link" href="%2$s"><i class="icon-rss"></i></a>', 'largo'), $posts_term, get_category_feed_link( get_queried_object_id() ) );
                    } elseif ( is_tag() ) {
                        printf(__('Recent %1$s<a class="rss-link" href="%2$s"><i class="icon-rss"></i></a>', 'largo'), $posts_term, get_tag_feed_link( get_queried_object_id() ) );
                    } elseif ( is_tax() ) {
                        $queried_object = get_queried_object();
                        $term_id = intval( $queried_object->term_id );
                        $tax = $queried_object->taxonomy;
                        printf(__('Recent %1$s<a class="rss-link" href="%2$s"><i class="icon-rss"></i></a>', 'largo'), $posts_term, get_term_feed_link( $term_id, $tax ) );
                    } elseif ( is_month() ) {
                        printf(__('Monthly Archives: <span>%s</span>', 'largo'), get_the_date('F Y') );
                    } elseif ( is_year() ) {
                        printf(__('Yearly Archives: <span>%s</span>', 'largo'), get_the_date('Y') );
                    } else {
                        _e('Archives', 'largo');
                    }
                    ?>
            </h3>

    <?php
            // and finally wind the posts back so we can go through the loop as usual

            rewind_posts();
query_posts('posts_per_page=1'); 
            while ( have_posts() ) : the_post();
                get_template_part( 'content', 'singleseries' );
            endwhile;

                    largo_content_nav( 'nav-below' );
        } else {
            get_template_part( 'content', 'not-found' );
        }       
    ?>

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

<?php get_sidebar(); ?>
<?php get_footer(); ?>

1 Answer 1

0

I don't know what largo_content_nav() does but you clobber the main query here:

query_posts('posts_per_page=1'); 

And then several lines later attempt to paginate (or so the function name and argument suggest):

largo_content_nav( 'nav-below' );

If largo_content_nav uses the global variable $wp_query which I kind think it must since you don't pass it a WP_Query object, then you are paginating based on an object that is different from the one used to load the page. That is:

  1. Long before the theme templates load, WordPress creates the $wp_query global based on various query parameters and rewrite rules.
  2. The page loads according to information in that query
  3. You, by using query_posts which you should never do, then overwrite $wp_query
  4. And attempt to paginate. Unfortunately you've altered the query at step #3. Things are out of sync.

I don't honestly understand (or I am not sure that I understand) what you are trying to accomplish with this block of code, but your comment below ...

All I needed to do was show 1 full post per page on the archives for this taxonomy term.

... makes me think that all you want to do is limit your archives to a single result per page. You have two loops on the page, but it seems that you only want one and have cooked up something odd and wildly complicated. I think what you need is:

function single_per_page_archive_wpse_133857($qry) {
  if ($qry->is_main_query() && $qry->is_archive()) {
    $qry->set('posts_per_page',1);
  }
}
add_action('pre_get_posts','single_per_page_archive_wpse_133857');

And remove most of this:

            // and finally wind the posts back so we can go through the loop as usual

            rewind_posts();
query_posts('posts_per_page=1'); 
            while ( have_posts() ) : the_post();
                get_template_part( 'content', 'singleseries' );
            endwhile;

Keeping just this (I think):

get_template_part( 'content', 'singleseries' );
2
  • So if I should never use that, and it's out of sync, where do I start to correct it? All I needed to do was show 1 full post per page on the archives for this taxonomy term. Commented Feb 8, 2014 at 14:31
  • @angiemeeker: see the edit
    – s_ha_dum
    Commented Feb 8, 2014 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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