The get_adjacent_image_link() WordPress PHP function retrieves the next or previous image link that has the same post parent.
Usage
get_adjacent_image_link( $prev, $size, $text );
Parameters
$prev
(bool) – Optional. Whether to display the next (false) or previous (true) link. Default is true.$size
(string|int) – Optional. Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default is ‘thumbnail’.$text
(bool) – Optional. Link text. Default is false.
More information
See WordPress Developer Resources: get_adjacent_image_link()
Examples
Display the previous image link
This code will display the previous image link in the default ‘thumbnail’ size.
echo get_adjacent_image_link( true );
Display the next image link
This code will display the next image link in the default ‘thumbnail’ size.
echo get_adjacent_image_link( false );
Display the previous image link with a custom size
This code will display the previous image link with a custom size of 300×300 pixels.
echo get_adjacent_image_link( true, array( 300, 300 ) );
Display the next image link with a registered image size
This code will display the next image link using the registered image size ‘medium’.
echo get_adjacent_image_link( false, 'medium' );
Display the previous image link with custom link text
This code will display the previous image link in the default ‘thumbnail’ size with custom link text “Previous Image”.
echo get_adjacent_image_link( true, 'thumbnail', 'Previous Image' );