显示具有相同后父级的下一个或上一个图像链接。

原型

adjacent_image_link( bool $prev = true, string|array $size = 'thumbnail', bool $text = false )

描述

从$ post global检索当前附件对象。

参数

$prev (bool) (Optional)  是否显示下一个(假)或上一个(真)链接。

$size (string|array) (Optional)  图片尺寸。接受任何有效的图像大小,或以像素为单位的宽度和高度值数组(按此顺序)。

$text (bool) (Optional)  链接文字。

源文件

路径:wp-includes/media.php

<?php
...
function 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 ( $attachment->ID == $post->ID ) {
			break;
		}
	}
	$output = '';
	$attachment_id = 0;
	if ( $attachments ) {
		$k = $prev ? $k - 1 : $k + 1;
		if ( isset( $attachments[ $k ] ) ) {
			$attachment_id = $attachments[ $k ]->ID;
			$output = wp_get_attachment_link( $attachment_id, $size, true, false, $text );
		}
	}
	$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'.
	 *
	 * @since 3.5.0
	 *
	 * @param string $output        Adjacent image HTML markup.
	 * @param int    $attachment_id Attachment ID
	 * @param string $size          Image size.
	 * @param string $text          Link text.
	 */
	echo apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text );
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/adjacent_image_link/