Using WordPress ‘get_adjacent_image_link()’ PHP function

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

This code will display the previous image link in the default ‘thumbnail’ size.

echo get_adjacent_image_link( true );

This code will display the next image link in the default ‘thumbnail’ size.

echo get_adjacent_image_link( false );

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 ) );

This code will display the next image link using the registered image size ‘medium’.

echo get_adjacent_image_link( false, 'medium' );

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' );