get_adjacent_image_link( bool $prev = true, string|int[] $size = 'thumbnail', bool $text = false ): string
- Since
- 5.8.0
- Source
wp-includes/media.php:3971
Description
Retrieves the current attachment object from the $post global.
Compatibility
- WordPress
- since 5.8.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$prevbooloptional- Whether to display the next (false) or previous (true) link. Default true.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' $textbooloptional- Link text. Default false.Default:
false
Return value
string- Markup for image link.
Performance profile
How much work a call to get_adjacent_image_link() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 37–66
- Plugin surface
- 1 hook
- Called by
- 3
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 68.
Third-party callbacks on '{$adjacent}_image_link' run inside this call, and their cost is not bounded by anything here.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach cache, option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_adjacent_image_link() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 37–51 | get_post(), post_parent(), get_children(), apply_filters() |
isset($attachments[$k]) | 57–66 | get_post(), post_parent(), get_children(), get_the_title(), wp_get_attachment_link(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 68 | 37–66 | 7 | |
| 8.5 | 68 | 37–66 | 7 | |
| 8.4 | 68 | 37–66 | 7 | |
| 8.3 | 68 | 37–66 | 7 | |
| 8.2 | 68 | 37–66 | 7 | |
| 8.1 | 68 | 37–66 | 7 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 70 | 37–67 | 7 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while get_adjacent_image_link() runs, in this order:
- apply_filters( {$adjacent}_image_link )filterline 4026 (+55 into the body)
Filters the adjacent image link.
Uses · 5
- get_post()Retrieves post data given a post ID or post object.
- get_children()Retrieves all children of the post parent ID.
- get_the_title()Retrieves the post title.
- wp_get_attachment_link()Retrieves an attachment page link using an image or icon, if possible.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 3
- adjacent_image_link()Displays next or previous image link that has the same post parent.
- get_next_image_link()Gets the next image link that has the same post parent.
- get_previous_image_link()Gets the previous image link that has the same post parent.
Source code
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 );}Changelog
Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/media.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.