Support » Themes and Templates » Display Page Navigation If More Than One Page

  • Resolved Connor Crosby

    (@ccmovies)


    I am currently making a theme that has custom navigation links for going through blog post lists (index.php). I would like to know how to create an if statement where it will only show the navigation if there is more than one page to go through? Right now it’s a little pointless if there is only one page. Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Andrew Nevins

    (@anevins)

    How do you create the navigation at the moment?

    Moderator Andrew Nevins

    (@anevins)

    You could wrap your navigation in a conditional statement;
    Exampled

    if ( get_next_posts_link() ) {
     // Your code that creates the next navigation
    }
    
    if ( previous_posts_link() ) {
     // Your code that creates the previous navigation
    }

    Of course replace the conditional functions exampled with relevance.

    Hi Andrew, thank you for your help. Unfortunately, that code will not work in my situation.

    Currently I have the following code:

    <nav class="navigation">
    
    	<div class="nav-count"><?php current_paged(); ?></div>
    
    	<div class="nav-links"><?php posts_nav_link('<span class="nav-sep">⋅</span>','« Previous Page','Next Page »'); ?></div>
    
    	<div class="sep"></div>
    
    </nav> <!-- end #navigation -->

    I would like this to only display if there is more than one page. Currently, the only thing that does not show is the previous and next link. But, I would like the whole .navigation div to not show if there is only one page. How can I do that?

    Moderator Andrew Nevins

    (@anevins)

    You could use a conditional statement around the entire navigation div.
    Something like

    if ( wp_count_posts > 1 ) {
    ?>
      <nav class="navigation">
    
    	<div class="nav-count"><?php current_paged(); ?></div>
    
    	<div class="nav-links"><?php posts_nav_link('<span class="nav-sep">⋅</span>','« Previous Page','Next Page »'); ?></div>
    
    	<div class="sep"></div>
    
      </nav> <!-- end #navigation -->
    <?php
    }
    ?>

    That sorta works. While it does remove the whole .navigation, now if there is more than one page it won’t display at all.

    Michael

    (@alchymyth)

    Forum Moderator

    this is the conditional statement used in Twenty Eleven:

    global $wp_query;
    	if ( $wp_query->max_num_pages > 1 ) : ?>

    used in your example:

    <?php global $wp_query; if ( $wp_query->max_num_pages > 1 ) : ?>
    
      <nav class="navigation">
    
    	<div class="nav-count"><?php current_paged(); ?></div>
    
    	<div class="nav-links"><?php posts_nav_link('<span class="nav-sep">⋅</span>','« Previous Page','Next Page »'); ?></div>
    
    	<div class="sep"></div>
    
      </nav> <!-- end #navigation -->
    
    <?php endif; ?>

    @alchymyth excellent! That worked! Next time I’ll look at the default themes before posting here 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display Page Navigation If More Than One Page’ is closed to new replies.