Im trying to make my next and previous post buttons display my custom field but for some reason it doesnt display the custom field its just blank. Heres my code.
<div id="next-prev-links">
<?php
$previous_post = previous_post_link('%link', ''.$image_prev.'', TRUE);
$next_post = next_post_link('%link',''.$image_next.'', TRUE);
$image_prev = get_post_meta( $previous_post->ID, 'image', true);
$image_next = get_post_meta( $next_post->ID, 'image', true);
?>
<?php if ( $image_prev != '' ) : ?>
<img src="<?php echo $image_prev; ?>" alt="" />
<?php endif; ?>
<?php if ( $image_next != '' ) : ?>
<img src="<?php echo $image_next; ?>" alt="" />
<?php endif; ?>
<?php previous_post_link('%link', 'Previous post', TRUE); ?>
<?php next_post_link('%link', 'Next post', TRUE); ?>
</div>
Here’s the code for what you’re trying to do (accessing post custom value):
I think you are trying to retrieve
ID
from link string that*_post_link()
functions return, rather than post object.If you need full object of next/previous post you should use
get_adjacent_post()
.PS it is good practice to develop with WP_DEBUG enabled, it makes much easier to catch type mismatches.