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.

Possible Duplicate:
Pagination not working with custom loop

I'm creating a custom page for logged in current users to view their posts. I would like to limit the number of displayed post to 5, but cannot get pagination to display.

This is what I'm using:

<?php    if ( is_user_logged_in() ):

global $current_user;
get_currentuserinfo();
$author_query = array('posts_per_page' => '5','author' => $current_user->ID);
$author_posts = new WP_Query($author_query);
while($author_posts->have_posts()) : $author_posts->the_post();
?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>       
<?php           
endwhile;

else :

echo "not logged in";

endif;
?>

I'm by no means professional, just enjoy creating my own sites, and so I be making a very silly nooby mistake. Please be gentle with me? Thank you for your help!

share|improve this question

marked as duplicate by Chip Bennett, toscho Nov 30 '12 at 18:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Hi Abby, did you try searching the site for related questions and answers? –  Chip Bennett Nov 30 '12 at 17:23
    
My apologies, Chip, but I have searched for related questions an answers. And while many tackle how to display authors posts for current users IDs, I could not find any that explain how to do this AND retain pagination... while the thread you've listed as duplicate, as the same title, to code is very different, and the solution does not work. I think you erroneously closed this thread. Could you please reopen it? –  Abby Ravera Dec 3 '12 at 8:48
add comment

1 Answer 1

Try reviewing this page on the codex. Using your above template, I would suggest the following:

<?php

if ( is_user_logged_in() ) :

    $args = array( 
        'posts_per_page'     => '5',
        'author'             => get_current_user_id(), // Removes a few lines of code ;)
    );

    $author_posts = new WP_Query( $args );
?>
    <?php /* Author has posts? */ ?>
    <?php if( $author_posts->have_posts() ) : ?>

        <?php /* Loop through author posts */ ?>
        <?php while( $author_posts->have_posts() ) : $author_posts->the_post(); ?>

            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>

        <?php endwhile; ?>

        <!-- Basic Pagination -->
        <div class="navigation"><p><?php posts_nav_link(); ?></p></div>

        <!-- Or using Wp Pagenavi (http://wordpress.org/extend/plugins/wp-pagenavi/) -->
        <?php if( function_exists( 'wp_pagenavi' ) ) wp_pagenavi( array( 'query' => $author_posts ) );

        <?php wp_reset_postdata(); /* Important after a custom query. */ ?>

    <?php else : ?>

        <p>Sorry you have no posts.</p>

    <?php endif; ?>


<?php else : ?>

    <p>Not logged in</p>

<?php endif; ?>
share|improve this answer
add comment

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