0

I use an extension called Custom Field Suite to import info from a csv in order to batch create posts. In order to display the form fields I use <?php echo CFS()->get('image_location'); ?> One type of information I import is image sources which I display images using <img src="<?php echo CFS()->get('image_location'); ?>" class="preview" alt="image thumbnail" /> This works great without any problems.

But, I want the images to link to the next post using next_post_link. I've seen <?php next_post_link('%link','<img src="IMAGELINK"/>'); ?> but I can't quite figure out how to combine the two bits of php into a whole.

I've tried <?php next_post_link('%link','<img src="echo CFS()->get('image_location')"/>'); ?> but get a syntax error. Any advice would be helpful.

1
  • You want the next post link to be an image?
    – Tunji
    Jan 5, 2017 at 21:43

2 Answers 2

2

You can format it as, there's also no need to put the echo

<?php next_post_link('%link',"<img src=" . CFS()->get('image_location') . "/>"); ?>

CFS()->get('image_location') gets the field based on the current post id. If you want to use it for the next or previous, you have to pass the next or previous posts ID.

You can do something like this:

$next_post = get_adjacent_post( true, '', false, 'taxonomy_slug' );
$next_post_id = '';
if ( is_a( $next_post, 'WP_Post' ) )
    $next_post_id = $next_post->ID;
if ( ! empty( $next_post_id ) ) {
    $next_post_image_source = CFS()->get( 'image_location', $next_post_id );
} else {
    $next_post_image_source = "default image";
}

next_post_link('%link',"<img src=" . $next_post_image_source . "/>"); 

No assurance this would work but it should be easy to continue from here

7
  • That fixes the syntax error but the image is not displayed at all. It's not seeing the img src tag. Jan 6, 2017 at 13:33
  • I've updated my code
    – Tunji
    Jan 6, 2017 at 14:08
  • It gets a syntax error on the { that comes before $next_post_image_source = CFS()->get( 'image_location', $next_post_id );. I've screwed around with it a bit but it keeps coming back to that {. Taking them out it errors on $next_post_image_source. Jan 6, 2017 at 20:00
  • The problem was the <?php php tags in-between the code. My bad
    – Tunji
    Jan 6, 2017 at 20:04
  • Yep, I caught that. Should have mentioned that. Still has that same error. Jan 6, 2017 at 20:17
0

I am able to get the desired functionality using the posts_nav_link template tag. posts_nav_link( '&nbsp;', '&nbsp;', "<img src=" . CFS()->get('artwork_image_location') . ">" ); It looks like the next_post_link template tag is not working for me at all, even with a hard link to an image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.