Home / custom field / Display custom field value of previous and next post
  • How to Display custom field value of previous and next post

    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.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    <?php
      $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; ?>
    

    ( WordPress codex functions, hooks, in this snippet. )

    get_next_post, get_post, get_previous_post, get_post_meta, _n,
  • Anonymous

    yeaaaaah,
    thanks for this code, i allways ask about this code, but finnaly found it her :)
    i can now creat my photo portfolio and to show the next or prev photo.
    Thanks a lot SIR.

  • That is what I needed! I added DVD covers as custom fields and I want to call them for navigation. that does the trick, thanks!

    • Ahhh very cool use of this snippet sounds great. Glad to hear you like the snippet.

    • Ahhh very cool use of this snippet sounds great. Glad to hear you like the snippet.

  • Bernard

    Thank you! Works perfectly!

  • wido

    Nice snippet

  • Pingback: Top 100+ websites about WordPress (part 1)()