For grabbing previous or next image link on attachment page we can use this https://codex.wordpress.org/Function_Reference/previous_image_link

How do we get ID for that attachment? Because I would like to get its alt title by using this echo get_post_meta(get_the_ID(), '_wp_attachment_image_alt', true); and put it below image, with maybe also a few more stuff.


previous_image_link looks like it uses adjacent_image_link.

You can probably do more with the post ID. This example shows how to get the previous and next post, then gets any image data out of the post thumbnail.

While this doesn't directly answer your question but I'm pretty confident you can use the ID to get what you need. Obviously this extra function requires a $post_id but you can pass get_post_ID() if you want or use get_previous_post() and get_next_post directly.

This sample was based off of the question Get Previous & Next posts by Post ID.

function get_next_post_id( $post_id, $is_next=true ) {
    return get_previous_post_id($post_id, ! $is_next );

function get_previous_post_id( $post_id, $is_prev=true ) {
    // Get a global post reference since get_adjacent_post() references it
    global $post;

    // Store the existing post object for later so we don't lose it
    $oldGlobal = $post;

    // Get the post object for the specified post and place it in the global variable
    $post = get_post( $post_id );

    // Get the post object for the previous post
    $previous_post = $is_prev ? get_previous_post() : get_next_post();

    // Reset our global object
    $post = $oldGlobal;

    if ( '' == $previous_post ) 
        return 0;

    return $previous_post->ID;

$post_id = 6331;
$prev_post_id = get_previous_post_id( $post_id ); 
$next_post_id = get_next_post_id( $post_id ); 

var_dump ( array(

    '****** POST ID',

    '****** POST TITLE',

    '****** IMAGE ID',
    $p_img_id = get_post_thumbnail_id( $prev_post_id ),
    $c_img_id = get_post_thumbnail_id( $post_id ),
    $n_img_id = get_post_thumbnail_id( $next_post_id ),

    '****** IMAGES',
    wp_get_attachment_image( $p_img_id ),
    wp_get_attachment_image( $c_img_id ),
    wp_get_attachment_image( $n_img_id ),

    '****** IMAGE META',
    wp_get_attachment_metadata( $p_img_id ),
    wp_get_attachment_metadata( $c_img_id ),
    wp_get_attachment_metadata( $n_img_id ),

    '****** IMAGE POST META',
    get_post_meta( $p_img_id ),
    get_post_meta( $c_img_id ),
    get_post_meta( $n_img_id ), 


  • Was this helpful at all? – jgraup Dec 11 '15 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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