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 istrue
.$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 isfalse
.
More information
See WordPress Developer Resources: adjacent_image_link()
Examples
Displaying the Next Image Link
// This code will display the next image link with a 'medium' size and no link text. adjacent_image_link(false, 'medium', false);
Displaying the Previous Image Link
// This code will display the previous image link with a 'thumbnail' size and no link text. adjacent_image_link(true);
Displaying the Next Image Link with Custom Size
// This code will display the next image link with a custom size of 300x200 pixels. adjacent_image_link(false, array(300, 200), false);
Displaying the Previous Image Link with Text
// This code will display the previous image link with 'thumbnail' size and "Previous Image" as link text. adjacent_image_link(true, 'thumbnail', 'Previous Image');
Displaying the Next Image Link with Text
// This code will display the next image link with 'medium' size and "Next Image" as link text. adjacent_image_link(false, 'medium', 'Next Image');