On this page
get_adjacent_image_link( bool $prev = true, string|int[] $size = 'thumbnail', bool $text = false ): string
Gets the next or previous image link that has the same post parent.
Description
Retrieves the current attachment object from the $post global.
Parameters
$prevbool Optional-
Whether to display the next (false) or previous (true) link.
Default:
true $sizestring|int[] Optional-
Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default
'thumbnail'.Default:
'thumbnail' $textbool Optional-
Link text.
Default:
false
Return
string Markup for image link.
Source
File: wp-includes/media.php. View all references
function get_adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) {
$post = get_post();
$attachments = array_values(
get_children(
array(
'post_parent' => $post->post_parent,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID',
)
)
);
foreach ( $attachments as $k => $attachment ) {
if ( (int) $attachment->ID === (int) $post->ID ) {
break;
}
}
$output = '';
$attachment_id = 0;
if ( $attachments ) {
$k = $prev ? $k - 1 : $k + 1;
if ( isset( $attachments[ $k ] ) ) {
$attachment_id = $attachments[ $k ]->ID;
$attr = array( 'alt' => get_the_title( $attachment_id ) );
$output = wp_get_attachment_link( $attachment_id, $size, true, false, $text, $attr );
}
}
$adjacent = $prev ? 'previous' : 'next';
/**
* Filters the adjacent image link.
*
* The dynamic portion of the hook name, `$adjacent`, refers to the type of adjacency,
* either 'next', or 'previous'.
*
* Possible hook names include:
*
* - `next_image_link`
* - `previous_image_link`
*
* @since 3.5.0
*
* @param string $output Adjacent image HTML markup.
* @param int $attachment_id Attachment ID
* @param string|int[] $size Requested image size. Can be any registered image size name, or
* an array of width and height values in pixels (in that order).
* @param string $text Link text.
*/
return apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text );
}
Hooks
- apply_filters( "{$adjacent}_image_link",
string $output ,int $attachment_id ,string|int[] $size ,string $text ) -
Filters the adjacent image link.
Related
Uses
| Uses | Description |
|---|---|
| get_children() wp-includes/post.php | Retrieves all children of the post parent ID. |
| wp_get_attachment_link() wp-includes/post-template.php | Retrieves an attachment page link using an image or icon, if possible. |
| get_the_title() wp-includes/post-template.php | Retrieves the post title. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
Used By
| Used By | Description |
|---|---|
| get_next_image_link() wp-includes/media.php | Gets the next image link that has the same post parent. |
| get_previous_image_link() wp-includes/media.php | Gets the previous image link that has the same post parent. |
| adjacent_image_link() wp-includes/media.php | Displays next or previous image link that has the same post parent. |
Changelog
| Version | Description |
|---|---|
| 5.8.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_adjacent_image_link