get_adjacent_post() – Function description, parameters and basic usage

Description:

get_adjacent_post – A WordPress function to retrieve previous or next post. It is mostly useful if you want to integrate go to next/previous post button in your WordPress theme.

Parameters:

$in_same_cat(optional)(boolean) – If you want to retrieve next/previous post from same category

$excluded_categories (optional)(string) – String of comma-separated category ids

$previous(optional)(boolean) – If you want to retrieve previous post

Basic usage examples:

<?php
$previousPost = get_adjacent_post(true, "", true));
$nextPost = get_adjacent_post(true);
?>

<?php if($previousPost) : ?>
    <a href="<?php echo get_permalink($previousPost->ID)?>">&laquo; Previous post</a>
<?php endif; ?>

<?php if($nextPost) : ?>
    <a href="<?php echo get_permalink($nextPost->ID)?>">Next: <?php echo $next->post_title; ?> &raquo;</a>
<?php endif; ?>
About Pavel Petrov 2 Articles |  21 How-tos
Pavel is a senior developer for the last 7 years, with extended interest in Linux administration, WordPress and Symfony.

Be the first to comment

Leave a Reply

Your email address will not be published.


*