返回介绍

adjacent_image_link()

发布于 2017-09-10 21:26:00 字数 3055 浏览 1120 评论 0 收藏 0

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

Displays next or previous image link that has the same post parent.


description

Retrieves the current attachment object from the $post global.


参数

$prev

(bool) (Optional) Whether to display the next (false) or previous (true) link.

Default value: true

$size

(string|array) (Optional) Image size. Accepts any valid image size, or an array of width and height values in pixels (in that order).

Default value: 'thumbnail'

$text

(bool) (Optional) Link text.

Default value: false


源代码

File: wp-includes/media.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 );
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/post.php: get_children()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post-template.php: wp_get_attachment_link()
  • wp-includes/media.php: {$adjacent}_image_link
  • wp-includes/post.php: get_post()

Used By

  • wp-includes/media.php: previous_image_link()
  • wp-includes/media.php: next_image_link()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文