WordPress上一篇和下一篇

Published
April 26, 2022
浏览次数 :  11

Wodpress next and previous post navigation is very important to increase visitor spending time on your site.

TO show the next and previous post we can use this code in any single.php page. First, let’s call 2 variableVairable

<?php 
$previous = get_previous_post();
$next = get_next_post();
 ?>

PHPCopy

Then use this code for previous postPrevious post

<?php if ( get_previous_post() ) {?>
<a href="<?php echo get_the_permalink($previous) ?>">
    <img src="<?php echo get_the_post_thumbnail_url($previous) ?>" alt="">
    <div>
        <h3> <?php echo get_the_title($previous) ?></h3>
        <p> PREV</p>
    </div>
</a>
<?php }?>

PHPCopy

This one for Next post of that post typeNext Post

<?php if ( get_next_post() ) {?>
<a href="<?php echo get_the_permalink($next) ?>">
    <img src="<?php echo get_the_post_thumbnail_url($next) ?>" alt="">
    <div>
        <h3> <?php echo get_the_title($next) ?></h3>
        <p>Next</p>
    </div>
</a>
<?php }?>

PHPCopy

That’s simple. It work on custom post single page too. 😉


Top