Using WordPress ‘adjacent_image_link()’ PHP function

The adjacent_image_link() WordPress PHP function displays the next or previous image link that shares the same post parent. It retrieves the current attachment object from the global $post.

Usage

adjacent_image_link( $prev, $size, $text );

In the above example, $prev determines whether to display the next (false) or previous (true) link. The $size parameter sets the image size, accepting any registered image size name, or an array of width and height values in pixels. The default value is ‘thumbnail’. $text sets the link text, with the default being false, indicating that no text will be displayed.

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: adjacent_image_link()

Examples

// This code will display the next image link with a 'medium' size and no link text.
adjacent_image_link(false, 'medium', false);
// This code will display the previous image link with a 'thumbnail' size and no link text.
adjacent_image_link(true);
// This code will display the next image link with a custom size of 300x200 pixels.
adjacent_image_link(false, array(300, 200), false);
// This code will display the previous image link with 'thumbnail' size and "Previous Image" as link text.
adjacent_image_link(true, 'thumbnail', 'Previous Image');
// This code will display the next image link with 'medium' size and "Next Image" as link text.
adjacent_image_link(false, 'medium', 'Next Image');