WordPress.org

Ready to get started?Download WordPress

Forums

Bizarre next_posts_link/previous_posts_link problem (8 posts)

  1. tactics
    Member
    Posted 4 years ago #

    Hi folks, got a weird problem I'm hoping someone can help me out with.

    To see the problem, go to this site and note the "previous post" link at the bottom of the entry:

    http://cinemagogue.com

    If you click on that link, it takes you to the next post and also gives you a previous post link, and you can navigate through all of the posts on the site as expected.

    BUT, if you click on one of the post links in the sidebar, the post doesn't have those links. The ul they are supposed to appear in is empty.

    Here's the code I'm using in both home.php and single.php:

    <?php if (show_posts_nav()) : ?>
    <div class="pagination">

      <li class="prevlink"><?php next_posts_link('Previous Post'); ?>
      <li class="nextlink"><?php previous_posts_link('Next Post'); ?>

    </div>
    <?php endif; ?>

    And here's the code from the functions.php file:

    function show_posts_nav() {
    global $wp_query;
    return ($wp_query->max_num_pages > 1);
    }

    Any ideas?

  2. That's probably not bizzare behavior; I think that's how your theme works with single posts that use page.php, as opposed to posts on single.php or home.php. Make a new page template for page.php and add the link code:

    http://codex.wordpress.org/Pages#Page_Templates

  3. tactics
    Member
    Posted 4 years ago #

    Page.php is used for permalink pages, not posts. So in the case of this site, it would be pages like:

    http://cinemagogue.com/about/

    But to test your theory, I added the code to page.php, and it didn't work.

  4. Hmm. Don't know. Do you have a posts loop on page.php?

  5. tactics
    Member
    Posted 4 years ago #

    Yep.

    <?php if(have_posts()) : ?>
    <?php while(have_posts()) : the_post(); ?>
    <h1 class="pageTitle"><?php the_title(); ?></h1>
    <?php the_content(); ?>
    <?php edit_post_link('Edit this page', '<p>', '</p>'); ?>
    <?php if (show_posts_nav()) : ?>
    <div class="pagination">

      <li class="prevlink"><?php next_posts_link('Previous Post'); ?>
      <li class="nextlink"><?php previous_posts_link('Next Post'); ?>

    </div>

  6. jcdesign
    Member
    Posted 4 years ago #

    This might not be quite your problem, but -

    I saw a similar probem addressed on another blog while trying to figure out why my custom query on a template page showed previous_posts_link, but not next_posts_link.

    You might try this for your next_posts_link code:

    <?php next_posts_link('next &raquo;', $wp_query->max_num_pages) ?>

    It worked in my case. HTH.

  7. Chrisdigital
    Member
    Posted 4 years ago #

    Hi folks, I'm using WP 2.9.1 I was having a similar issue to this guy...

    http://wordpress.org/support/topic/232483?replies=2

    I applied css styling to the li's and then I saw they were showing up visually when not necessary with no text in them yet. So I needed conditional code for them to show up...

    I ended up combining these two concepts:
    http://www.ericmmartin.com/conditional-pagepost-navigation-links-in-wordpress-redux/

    http://digwp.com/2009/12/optimizing-wordpress-post-navigation/

    to check for 3 specific cases:

    1. I am NOT on the first page of posts, and they're more posts to looks at.

    2. Am I at the end?

    3. I'm on the first page.

    I commented out code, that other people might need:

    <?php if (($paged < $wp_query->max_num_pages) && ($paged > 1)) {// this checking max # of pages that are NOT page 1 ?>
    
    					<div class="navigation">
    						<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    						<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    					</div>
    
    				<?php } elseif ($wp_query->max_num_pages == $paged) {//this is checking for the LAST page in pagination ?>
    
    						<div class="navigation">
    							<!--<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>-->
    							<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    						</div>
    
    				<?php } else {// this is checking for the 1ST page in pagination ?>
    
    					<div class="navigation">
    						<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    						<!--<div class="alignright"><a href="<?php bloginfo('url'); ?>/archives/">Site Archives &raquo;</a></div>-->
    					</div>
    
    				<?php } ?>

    I know this is a bit inelegant, but it works.

  8. lynxfx
    Member
    Posted 4 years ago #

    @chrsdgtl

    Thank you so much for that! I registered just to post this. I've been working all day trying to figure out how to do something as simple as having customized "next" or "prev" links not display on the first or last page. I even came across those sites you linked but could never get it 100%. Your post had me up and running in 10 seconds.

    If anyone is using images or CSS sprite style rollovers for their previous_post_link and next_posts_link then this is the only code you need.

Topic Closed

This topic has been closed to new replies.

About this Topic