Home > Software engineering >  How to write PHP condition for Next & Previous post?
How to write PHP condition for Next & Previous post?

Time:05-04

I don't know how to write if condition statement in this situation? Need some assistance. I want to completely hide a div if they don't have any next or previous posts available to show? And cover the remaining space with another available div. Hope it makes sense. Please see my final code below.

<div id="post-navigation">
    
    <?php
        $prev_post = get_previous_post(); 
        $prev_id = $prev_post->ID ;
        $prev_permalink = get_permalink( $prev_id );
        $prev_thumbnail =  get_the_post_thumbnail_url( $prev_id );
        $prev_image_alt = get_post_meta($prev_thumbnail, '_wp_attachment_image_alt', true); 
        $next_post = get_next_post();
        $next_id = $next_post->ID ;
        $next_permalink = get_permalink($next_id);
        $next_thumbnail =  get_the_post_thumbnail_url( $next_id );
        $next_image_alt = get_post_meta($next_thumbnail, '_wp_attachment_image_alt', true);
    ?>
    

    <article >
        <div >
            <a href="<?php echo $prev_permalink; ?>">
                <img src="<?php echo $prev_thumbnail; ?>" alt="<?php echo $prev_image_alt; ?>" width="200" height="200">
            </a>    
        </div>
        <header >
             <div >
                 <span><?php previous_post_link( '%link', __( '<span >&larr;</span> Previous', 'twentyeleven' ) ); ?> 
                 </span>
             </div>  
            <h5 ><a href="<?php echo $prev_permalink; ?>"><?php echo $prev_post->post_title; $short_title; ?></a></h5>
        </header>
    </article>

    <article >
        <div >
            <a href="<?php echo $next_permalink; ?>">
                <img src="<?php echo $next_thumbnail; ?>" alt="<?php echo $next_image_alt; ?>" width="200" height="200">
            </a>    
        </div>
        <header >
             <div >
                 <span><?php next_post_link( '%link', __( 'Next <span >&rarr;</span>', 'twentyeleven' ) ); ?> 
                 </span>
             </div>  
            <h5 ><a href="<?php echo $next_permalink; ?>"><?php echo $next_post->post_title; ?></a></h5>
        </header>
    </article>      

</div>

enter image description here enter image description here

CodePudding user response:

Here is an example to completely hide the post-navigation div, if there is no next post. You should be able to figure out the rest of what you want from this example.

This uses the is_a function, since the get_next_post either returns an empty string, null, or a post object so if you check if the value is a WP_POST object you can show or hide your div as you want. Always remember to escape your output also.

<?php
$next_post = get_next_post();
// check to see if $next_post is something or nothing.
if ( is_a( $next_post, 'WP_POST' ) ) :
    $next_id        = $next_post->ID;
    $next_permalink = get_permalink( $next_id );
    $next_thumbnail = get_the_post_thumbnail_url( $next_id );
    $next_image_alt = get_post_meta( $next_thumbnail, '_wp_attachment_image_alt', true );
?>
<div id="post-navigation">
    <article >
        <div >
            <a href="<?php echo esc_url( $next_permalink ); ?>">
                <img src="<?php echo esc_url( $next_thumbnail ); ?>" alt="<?php echo esc_attr( $next_image_alt ); ?>" width="200" height="200">
            </a>
        </div>
        <header >
            <div >
                <span><?php next_post_link( '%link', __( 'Next <span >&rarr;</span>', 'twentyeleven' ) ); ?></span>
            </div>
            <h5 >
                <a href="<?php echo esc_url( $next_permalink ); ?>"><?php echo esc_attr( $next_post->post_title ); ?></a>
            </h5>
        </header>
    </article>
</div>
<?php endif; ?>
  • Related