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 am using the Starkers theme as a foundation to build my theme and I am having difficulty getting my loop.php to paginate posts properly. When I click the "Next" or "Previous" post pagination button, it succeeds in changing the URL to example.com/page/2 however it does not actually load new posts (I have dozens of posts with the category I am querying, so this is not the problem.)

Here is the code I have in my loop.php file. Please let me know if you need me to clarify anything further.

                <?php include('includes/featured-content-slider.php'); ?>

            <!-- 960 16 Column Grid -->
            <div class="container_16"> 

                <!-- Featured News -->
                <section class="grid_11 featured-news">

                    <!-- Featured News Heading -->
                    <h1></h1>
                    <!-- /Featured News Heading -->

                        <!-- Featured News Loop -->
                        <?php query_posts('category_name=featured-news'); ?>
                            <?php while ( have_posts() ) : the_post(); ?>
                        <!-- Featured News Loop -->

                        <!-- Post --> 
                        <article class="post" id="post-<?php the_ID(); ?>" <?php post_class(); ?> >

                            <!-- Featured News Title -->
                            <span><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                            </span>
                            <!-- /Featured News Title -->

                            <!-- Featured News Meta -->
                            <p class="featured-news-post-meta">By <span class="featured-news-author"><?php echo get_the_author(); ?></span> / <?php echo get_the_date('d.m.Y'); ?>
                            </p>
                            <!-- /Featured News Meta -->

                            <!-- Featured News Thumbnail -->
                            <?php the_post_thumbnail(array(640,320)); ?>
                            <!-- /Featured News Thumbnail -->

                            <!-- Featured News Excerpt -->
                            <p class="featured-news-excerpt"><?php the_excerpt(); ?></p>
                            <!-- /Featured News Excerpt -->

                            <!-- Featured News Social Links -->
                            <ul class="featured-news-social-media-links">
                                <a href="#"><li class="facebook-share-post"></li></a>
                                <a href="#"><li class="twitter-post"></li></a>
                                <a href="#"><li class="google-plus-post"></li></a>
                                <a href="#"><li class="facebook-like-post"></li></a>
                                <a href="<?php the_permalink(); ?>"><li class="comment-post"></li></a>
                            </ul>
                            <!-- /Featured News Social Links -->

                        </article>
                        <!-- /Post -->

                            <?php endwhile; ?>
                            <!-- /Featured News Loop -->

                    <!-- Pagination -->
                    <?php if ($wp_query->max_num_pages > 1) : ?>
                        <div class="pagination grid_3">
                            <?php next_posts_link( __( '&larr; Older News', 'twentyten' ) ); ?>
                        </div>
                        <div class="pagination grid_3 push_6">
                            <?php previous_posts_link( __( 'Newer News &rarr;', 'twentyten' ) ); ?>
                        </div>
                    <?php endif; ?>
                    <!-- /Pagination -->

                </section>
                <!-- /Featured News -->

                <!-- Other News -->
                <aside class="grid_5 push_1 other-news">

                    <!-- Other News Heading -->
                    <h3></h3>
                    <!-- /Other News Heading -->

                        <!-- Other News Loop -->
                        <?php query_posts('category_name=other-news&showposts=6'); ?>
                        <?php while (have_posts()) : the_post(); ?>

                        <!-- Other News List -->
                        <ul>
                            <li><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                            </li>
                            <li class="other-news-post-date"><?php echo get_the_date('d.m.Y'); ?>
                            </li>
                        </ul>
                        <!-- /Other News List -->

                        <?php endwhile; ?>
                        <?php wp_reset_query(); ?>
                        <!-- /Other News Loop -->

                        <!-- View More -->
                        <div class="view-more">
                            <a href="/other-news">View More &rarr;</a>
                        <div>
                        <!-- /View More -->

                </aside>
                <!-- /Other News -->

            </div>
            <!-- /960 16 Column Grid -->
share|improve this question
    
Did you try searching the site before posting? The question of custom loop pagination has been asked several times already. –  Chip Bennett Jun 9 '12 at 17:08
    
possible duplicate of Pagination not working with custom loop –  Chip Bennett Jun 9 '12 at 17:09
add comment

1 Answer

I don't know what's inside featured-content-slider.php, but I'm assuming this is the loop you'd like to paginate:

query_posts('category_name=featured-news');

The issue is that by calling query_posts like this in the template, you're overwriting the original query for the page and not setting any page parameter. Your two options are to either modify the original query string, stored in the global variable query_string:

query_posts( $query_string . '&category_name=featured-news' );

or a better method is to modify the query with an action hook before it happens, so you're not simple overwriting the original. This example would go in your theme's functions.php, here we check if it's the home page and modify the query by setting category name:

function wpa54691_filter_pre_get_posts( $query ) {
    if ( $query->is_home ) {
        $query->set( 'category_name', 'featured-news' );
    }
    return $query;
}
add_filter( 'pre_get_posts', 'wpa54691_filter_pre_get_posts' );

For additional queries, like this one further down:

query_posts('category_name=other-news&showposts=6');

You should use a new instance of WP_Query instead.

$args = array(
    'posts_per_page' => 6,
    'category_name' => 'other-news'
);
$other_news = new WP_Query( $args );
while( $other_news->have_posts() ):
    $other_news->the_post();
    $the_title();
endwhile;

If you're also using query_posts within featured-content-slider.php, you'll need to change that as well so as not to interfere with your primary loop.

share|improve this answer
add comment

Your Answer

 
discard

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