WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display Page Navigation If More Than One Page (8 posts)

  1. Connor Crosby
    Member
    Posted 1 year ago #

    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!

  2. Andrew
    Forum Moderator
    Posted 1 year ago #

    How do you create the navigation at the moment?

  3. Andrew
    Forum Moderator
    Posted 1 year ago #

    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.

  4. Connor Crosby
    Member
    Posted 1 year ago #

    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?

  5. Andrew
    Forum Moderator
    Posted 1 year ago #

    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
    }
    ?>
  6. Connor Crosby
    Member
    Posted 1 year ago #

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

  7. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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; ?>
  8. Connor Crosby
    Member
    Posted 1 year ago #

    @alchymyth excellent! That worked! Next time I'll look at the default themes before posting here :)

Topic Closed

This topic has been closed to new replies.

About this Topic