0

i have the following code to get the next and the previous article.

if(wp_get_post_parent_id($post->ID)){
    $pagelist = get_pages('post_parent='.wp_get_post_parent_id($post->ID));
    $pages = array();
    foreach ($pagelist as $page) {
    $pages[] += $page->ID;
    }

    $current = array_search($post->ID, $pages);
    $prevID = $pages[$current-1];
    $nextID = $pages[$current+1];


}

With the $nextID and $previID I am able to create a link like here:

<a title="<?php echo get_the_title( $prevID ); ?>" href="<?php echo get_permalink($prevID); ?>"><?php echo get_the_title( $prevID ); ?> &raquo;</a>

Now i want to also have the featured image of this $prevID and $nextID. I don't know why, but it does not work. I tried the following:

echo wp_get_attachment_url(get_post_thumbnail_id($prevID));

How can i do that? Even if i add the fix ID like 1253 instead of $prevID, it does not work. When i use $post->ID (of the current article, just to check), it works.

Any idea?

2 Answers 2

1

First you have to make sure the current post ID + 1 and Current post ID - 1 is exactly the post ID of the next and previous posts.

if it is correct then you can use

<img src="<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id($prevID), 'full'); echo $image[0];?>" />

and

<img src="<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id($nextID), 'full'); echo $image[0];?>" />

The wp_get_attachment_image_src returns an array with image url as the first element of the array

2
  • That works. Great. Thx.
    – rezwiebel
    Commented Nov 3, 2016 at 12:20
  • @rezwiebel : glad to know it helped you. please up vote and accept answer if worked Commented Nov 3, 2016 at 12:36
1

Are you sure $prevID even contains an ID? Try to echo it out first.

If so, the following code will output the full <img src="" /> tag to display the image. The second parameter is the size you want.

echo get_the_post_thumbnail( $prevID, 'post-thumbnail' );

get_the_post_thumbnail

Your Answer

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

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