0

Ive created an author.php to show the author's posts, with pagination. When clicked the next button, takes me to page two but not using the same template. Any reason?

My query in author.php

$curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
  'author'        =>  $curauth->ID,
  'orderby'       =>  'post_date',
  'order'         =>  'DSC',
  'paged'         => $paged,
  'post_type' => 'post',
  'posts_per_page' => 1, // there are more
  );

  $authors_posts = new WP_Query( $args );

Links:

<?php echo get_next_posts_link( 'Next', $authors_posts->max_num_pages ); ?>
<?php echo get_previous_posts_link( 'Previous' ); ?>
5
  • 1
    Why do you create a new query in the template rather than use the default main query? – Milo Jul 31 '17 at 12:00
  • Hi @Milo. Im new to wordpress so I thought everything uses WP_Query. – Sylar Jul 31 '17 at 12:01
  • 1
    WordPress generates the main query for you, look at any of the built in themes, none have queries in the template. Search for pre_get_posts if you wish to alter the main query parameters. – Milo Jul 31 '17 at 12:04
  • Also, which template does it load on page 2? Does it load the same template but with different content? Be crystal clear and precise so we don't misunderstand and give the wrong answer, you can use the query monitor plugin to find out what it loaded and why ( and look into pre_get_posts, you're correct it does use WP_Query, but why throw away the first query? It's wasteful, just modify it instead via the filter – Tom J Nowell Jul 31 '17 at 12:16
  • @TomJNowell It uses the index.php for page 2, 3 etc – Sylar Jul 31 '17 at 12:18
0

This is not fix for your issue is only an working example of what you can use for the author.php page.

<?php 
 $number   = 10;
 $paged    = (get_query_var('paged')) ? get_query_var('paged') : 1;
 $offset   = ($paged - 1) * $number;
 $users    = get_users();
 $query    = get_users('&offset='.$offset.'&number='.$number);
 $total_users = count($users);
 $total_query = count($query);
 $total_pages = intval($total_users / $number) + 1;

 echo '<ul id="users">';

 foreach($query as $q) { ?>

 <li class="user clearfix">
   <div class="user-avatar">
    <?php echo get_avatar( $q->ID, 80 ); ?>  
  </div>
  <div class="user-data">

  <h4 class="user-name">
    <a href="<?php echo get_author_posts_url($q->ID);?>">
      <?php echo get_the_author_meta('display_name', $q->ID);?>
    </a>
  </h4>

     <?php if (get_the_author_meta('description', $q->ID) != '') : ?>
    <p><?php echo get_the_author_meta('description', $q->ID); ?></p>
    <?php endif; ?>

   </div>
  </li>

  <?php } 

  echo '</ul>';

?>

<?php
  if ($total_users > $total_query) {
  echo '<div id="pagination" class="clearfix">';
  echo '<span class="pages">Pages:</span>';
  $current_page = max(1, get_query_var('paged'));
  echo paginate_links(array(
  'base' => get_pagenum_link(1) . '%_%',
  'format' => 'page/%#%/',
  'current' => $current_page,
  'total' => $total_pages,
  'prev_next'    => false,
  'type'         => 'list',
  ));
  echo '</div>';
?>

You will also need to adjust the css classes to match your theme styling

3
  • Can you explain how this answers the question and why it fixes the issue? – Tom J Nowell Jul 31 '17 at 12:17
  • @TomJNowell yes you right I should have post it a fix for his solution instead of a new author.php page - although my author.php page code will work and therefore will resolve his issue :) – Nuno Sarmento Jul 31 '17 at 12:21
  • Well, the task was to display all the author's post on /author/name/ url. WP confuses me. Is there another page/template to achieve this? – Sylar Jul 31 '17 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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