get_adjacent_image_link

函数
get_adjacent_image_link ( $prev = true, $size = 'thumbnail', $text = false )
参数
  • (bool) $prev Optional. Whether to display the next (false) or previous (true) link. Default true.
    Required:
    Default: true
  • (string|int[]) $size Optional. Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default 'thumbnail'.
    Required:
    Default: 'thumbnail'
  • (bool) $text Optional. Link text. Default false.
    Required:
    Default: false
返回值
  • (string) Markup for image link.
定义位置
相关方法
adjacent_image_linkget_next_image_linkget_adjacent_post_linkget_adjacent_post_rel_linkwp_get_attachment_image_url
引入
5.8.0
弃用
-

get_adjacent_image_link: 这个函数用来获取画廊中下一个或上一个图片附件的链接。

获取具有相同文章父级的下一个或上一个图片链接。

从$post全局中检索当前的附件对象。

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 );
}

常见问题

FAQs
查看更多 >