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'm trying to create pagination on a blog post page where one page displays 13 blog posts and then displays some links to go to another page. Now just to get it working, I'm using this code:

<?php
/*
Template Name: Blog
*/
?>

<?php get_header(); ?>

        <div id="content">

            <div id="inner-content" class="wrap clearfix">

                <div id="main" class="eightcol clearfix" role="main">

                <div class="homeNewestListings">

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

                    <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">

                        <header>

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

                        </header> <!-- end article header -->

                        <section class="post-content">

                        </section> <!-- end article section -->

                        <footer>

                            <p class="clearfix"><?php the_tags('<span class="tags">Tags: ', ', ', '</span>'); ?></p>

                        </footer> <!-- end article footer -->

                    </article> <!-- end article -->


                    <?php endwhile; ?>  

                    <?php else : ?>

                    <article id="post-not-found">
                        <header>
                            <h1>Not Found</h1>
                        </header>
                        <section class="post-content">
                            <p>Sorry, but the requested resource was not found on this site.</p>
                        </section>
                        <footer>
                        </footer>
                    </article>

                    <?php endif; ?>

                </div> <!-- end .homeNewestListings -->

                    <?php $horizontalRuleCounter = 0; ?>
                    <?php $verticalRuleCounter = 1; ?>
                    <?php $postCounter = 0; ?>

                <?php $args = array( 'category_name=blog', 'posts_per_page' => 13 );
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post();
                        if ($postCounter == 0) { ?>
                            <div id="firstBlogPost">
                                <h1 class="h2"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                                <p class="meta"><time datetime="<?php echo the_time('Y-m-j'); ?>" pubdate><?php the_time('F jS, Y'); ?></time> <?php _e("by", "bonestheme"); ?> <?php the_author_posts_link(); ?></p>
                                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_post_thumbnail('bones-thumb-465'); ?></a>
                                <?php the_excerpt(); 
                                $postCounter = 1;
                        echo '</div>';
                        } else {
                        echo '<div id="otherBlogPosts">'; ?>
                            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_post_thumbnail('bones-thumb-300'); ?></a>
                            <h1 class="h2"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                            <p class="meta"><time datetime="<?php echo the_time('Y-m-j'); ?>" pubdate><?php the_time('F jS, Y'); ?></time> <?php _e("by", "bonestheme"); ?> <?php the_author_posts_link(); ?></p>

                            <?php
                                echo '<div id="agentTitle">';
                                meta('agent-title'); 
                                echo '</div>';
                            the_excerpt();
                         echo '</div>';     
                            if ($verticalRuleCounter % 2) { 
                                ?>
                                <div class="verticalRule"></div>
                                <?php
                            } else {
                            }                            

                            $verticalRuleCounter = $verticalRuleCounter +1; 

                        if ($horizontalRuleCounter % 2) { 
                                ?><img class="responsive" src="<?php echo get_template_directory_uri(); ?>/images/horizontalRule.png"><?php
                            }
                            $horizontalRuleCounter = $horizontalRuleCounter +1;
                            }
                    endwhile; ?>
                    <? // php wp_pagenavi(); ?>
<?php

global $paged;
$temp = $wp_query;
$wp_query = null;

$wp_query = new WP_Query( array('post_type' => 'post','posts_per_page' => 2, 'paged' =>     $paged ) ); 
while (have_posts()) : the_post(); 
// Your post content.
endwhile;
echo paginate_links( $args );
global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text'    => __('Previous'),
'next_text'    => __('Next')
) );

?>                          
                </div> <!-- end #main -->

                <?php get_sidebar('blog'); // sidebar blog ?>

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

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

Now it displays the links but when I click to go to the next page, it change the url but it still is displaying the posts from the first page.

What am I doing wrong?

Thanks in advance!

share|improve this question
    
I'd guess that you've modifying the query in the template and aren't passing any pagination parameters to the query. if you want to modify a particular query (like for a different posts_per_page amount) use the pre_get_posts action rather than doing it in the template. –  Milo Jul 31 '13 at 21:26
    
I've edited the code so you can see if I missed something. Thanks. –  graphical_force Jul 31 '13 at 21:55

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.