WordPress.org

Ready to get started?Download WordPress

Forums

Conditional to see if there is a next or previous post (9 posts)

  1. cfheinen
    Member
    Posted 4 years ago #

    I want to modify my current post navigation with custom arrows instead of the default « . However, when I create a css style with arrows or use a syntax other than next_posts_link('« Older Entries'), I get unwanted arrows with no link. Any idea how show custom arrows only if there is a next or previous post? Some kind of conditional statement?

    This is what I am doing so far (the ".left a" and ".right a" are styles with padding and background images for arrows):

    <div class="navigation clearfix">
    
    	<?php $previous_post = get_previous_post();?>
    	<?php $gallery = get_post_meta($previous_post->ID, 'Thumbnail', $single = true); ?>
    	<?php $title = $previous_post->post_title; ?>
    	<?php $link = get_permalink( $previous_post->ID ); ?>
    	<p class="left" ><a href="<?php echo $link ?>" class="screenshot" title="" rel="<?php echo $gallery ?>"><?php echo $title ?></a></p>
    
            <?php $next_post = get_next_post(); ?>
    	<?php $gallery = get_post_meta($next_post->ID, 'Thumbnail', $single = true); ?>
    	<?php $title = $next_post->post_title; ?>
    	<?php $link = get_permalink( $next_post->ID ); ?>
    	<p class="right" ><a href="<?php echo $link ?>" class="screenshot" title="" rel="<?php echo $gallery ?>"><?php echo $title ?></a></p>
    
    </div>
  2. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    try to use the conditional statements:

    if(get_next_posts_link()) { } else { }

    and

    if(get_previous_posts_link()) { } else { }

  3. cfheinen
    Member
    Posted 4 years ago #

    Thanks for the quick response! I tried using get_next_posts_link() in the conditional but it goes to the else regardless of whether there is a link or not. I am probably writing the wrong syntax. What do you think?

    <div class="navigation clearfix">
    
    									<?php if(get_previous_posts_link()) : ?>
    										<?php $previous_post = get_previous_post();?>
    										<?php $gallery = get_post_meta($previous_post->ID, 'Thumbnail', $single = true); ?>
    										<?php $title = $previous_post->post_title; ?>
    										<?php $link = get_permalink( $previous_post->ID ); ?>
    										<p class="left" ><a href="<?php echo $link ?>" class="screenshot" title="" rel="<?php echo $gallery ?>"><?php echo $title ?></a></p>
    									<?php else : ?>
    										<p class="left">it worked on the left</p>
    									<?php endif; ?>
    
    									<?php if(get_next_posts_link()) : ?>
    										<?php $next_post = get_next_post(); ?>
    										<?php $gallery = get_post_meta($next_post->ID, 'Thumbnail', $single = true); ?>
    										<?php $title = $next_post->post_title; ?>
    										<?php $link = get_permalink( $next_post->ID ); ?>
    
    										<p class="right" ><a href="<?php echo $link ?>" class="screenshot" title="" rel="<?php echo $gallery ?>"><?php echo $title ?></a></p>
    
    									<?php else : ?>
    										<p class="right">it worked on the right</p>
    									<?php endif; ?>
    
    								</div>
  4. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    was not quite clear if you wanted the code for index.php, or if you are in single.php?

    if in single.php, then you might want to use 'get_next_post_link()' ( no s after post )

  5. cfheinen
    Member
    Posted 4 years ago #

    It was for single.php, let me give that a try. Thanks!

  6. cfheinen
    Member
    Posted 4 years ago #

    Didn't quite work, the following code returned the two else statements and previous_post_link. It should have returned my custom link or nothing if there was no next/previous page.

    <div class="navigation clearfix">
    
    									<?php if(previous_post_link() != "") : ?>
    	<?php $previous_post = get_previous_post();?>
    	<?php $gallery = get_post_meta($previous_post->ID, 'Thumbnail', $single = true); ?>
    	<?php $title = $previous_post->post_title; ?>
    	<?php $link = get_permalink( $previous_post->ID ); ?>
            <p class="left" ><a href="<?php echo $link ?>" class="screenshot" title="" rel="<?php echo $gallery ?>"><?php echo $title ?></a></p>
    									<?php else : ?>
    	<p class="left">it worked on the left</p>
    	<?php endif; ?>
    
    									<?php if(next_post_link() != "") : ?>
    	<?php $next_post = get_next_post(); ?>
    	<?php $gallery = get_post_meta($next_post->ID, 'Thumbnail', $single = true); ?>
    	<?php $title = $next_post->post_title; ?>
    	<?php $link = get_permalink( $next_post->ID ); ?>
            <p class="right" ><a href="<?php echo $link ?>" class="screenshot" title="" rel="<?php echo $gallery ?>"><?php echo $title ?></a></p>
    									<?php else : ?>
    	<p class="right">it worked on the right</p>
    	<?php endif; ?>
    
    </div>

    Am I writing my if statement wrong?

    <?php if(previous_post_link() != "") : ?>
        Do X
    <?php else : ?>
        Do Y
    <?php endif; ?>
  7. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    i found out today that there is no 'get_next_post_link()' :-(

    this is something else to try:
    based on: http://digwp.com/2009/12/optimizing-wordpress-post-navigation/

    <?php if(get_adjacent_post(false, '', false)) {
    /*show custom next link*/
    }
    else { /*do nothing*/ } ; ?>

    and

    <?php if(get_adjacent_post(false, '', true)) {
    /*show custom previous link*/
    }
    else { /*do nothing*/ } ; ?>

    i have this implemented on my own blog on single.php.

  8. Shane G.
    Member
    Posted 4 years ago #

  9. cfheinen
    Member
    Posted 4 years ago #

    Success!!! Thank you all for your help! Alchymyth your conditional statements using get_adjacent_post did the trick.

    If you want to see the end result, go down to the end of this post and check out the previous/next links. You guys rock!

    http://www.heinencreative.com/archives/articles/iui-vs-jqtouch/

Topic Closed

This topic has been closed to new replies.

About this Topic