0

So I'm working on a custom pagination following code:

<?php if ($wp_query->max_num_pages > 1) : ?>
<?php $next = get_next_posts_link( 'Previous', $the_query->max_num_pages );
$previous = get_previous_posts_link( 'Next' );
if(!empty($next)) {
  $links[] = '<li class="previous">'.$next.'</li>';
}
if(!empty($previous)) {
  $links[] = '<li class="next">'.$previous.'</li>';
}
if (!empty($links)) { ?>
  <nav class="post-nav clear clearfix">
    <ul><?php
      echo implode($links); ?>
    </ul>
  </nav><?php
} ?>
<?php endif; ?>

The pagination is working as expected. However, I would like to add a class to the nav depending depending if both "Previous" and "Next" links are displayed at the same time or not.

If both links are present the output looks like this (this is what it should look like)

enter image description here

But if there's only one link it looks funny.

enter image description here

By being able to add a class to the nav I could change the CSS

Any ideas how I could do this?

1

Well... pretty much just do exactly what you said. Add a class conditionally based on the number of links O.o

$link_container_class = '';

if (!empty($links)) { 
  if( 1 === count( $links ) ) {
    $link_container_class = 'single';
  }
  ?>
  <nav class="post-nav clear clearfix <?php echo $link_container_class; ?>">
    <ul><?php
      echo implode($links); ?>
    </ul>
 </nav><?php
} ?>
  • That's awesome! It worked right away! – Johann Dec 4 '15 at 6:27

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.