Display custom field value of previous and next post

Adding this snippet within the loop of your wordpress theme will display the custom field value from the previous and next posts. Just change the CUSTOM_FIELD on line 4 and 5 to the name of the custom field you wish to display.



$previous_post = get_previous_post();
$next_post = get_next_post();
$prev_value = get_post_meta( $previous_post->ID, 'CUSTOM_FIELD', $single = true);
$next_value = get_post_meta( $next_post->ID, 'CUSTOM_FIELD', $single = true);
<?php if ( $prev_value != '' ) : ?>
<p><?php echo $prev_value; ?></p>
<p><?php echo $next_value; ?></p>
<?php endif; ?>

Leave a comment