PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 16, 2022

[FIXED] How to write PHP condition for Next & Previous post?

 May 16, 2022     if-statement, php, wordpress     No comments   

Issue

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 class="pexel-previous-post-data has-post-thumbnail pexel-box">
        <div class="pexel-nextPrev-thumbnail">
            <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 class="pexel-previous-post-text">
             <div class="entry-meta before-title prev-next-pill">
                 <span><?php previous_post_link( '%link', __( '<span class="meta-nav">&larr;</span> Previous', 'twentyeleven' ) ); ?> 
                 </span>
             </div>  
            <h5 class="pxl_head--h5 pxl_heading_anim_underline"><a href="<?php echo $prev_permalink; ?>"><?php echo $prev_post->post_title; $short_title; ?></a></h5>
        </header>
    </article>

    <article class="pexel-next-post-data has-post-thumbnail pexel-box">
        <div class="pexel-nextPrev-thumbnail">
            <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 class="pexel-next-post-text">
             <div class="entry-meta before-title next-next-pill">
                 <span><?php next_post_link( '%link', __( 'Next <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?> 
                 </span>
             </div>  
            <h5 class="pxl_head--h5 pxl_heading_anim_underline"><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


Solution

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 class="pexel-next-post-data has-post-thumbnail pexel-box">
        <div class="pexel-nextPrev-thumbnail">
            <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 class="pexel-next-post-text">
            <div class="entry-meta before-title next-next-pill">
                <span><?php next_post_link( '%link', __( 'Next <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?></span>
            </div>
            <h5 class="pxl_head--h5 pxl_heading_anim_underline">
                <a href="<?php echo esc_url( $next_permalink ); ?>"><?php echo esc_attr( $next_post->post_title ); ?></a>
            </h5>
        </header>
    </article>
</div>
<?php endif; ?>


Answered By - Howard E
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing