WordPress開發(fā)函數(shù)adjacent_image_link()
WordPress開發(fā)函數(shù)adjacent_image_link(),顯示下一個(gè)或上一個(gè)圖像鏈接,該鏈接具有相同的文章父。
用法:
adjacent_image_link( bool $prev = true, string|int[] $size = 'thumbnail', bool $text = false )
描述:
從$post全局變量中檢索當(dāng)前附件對(duì)象。
參數(shù):
$prev
(bool) (可選) 是否顯示下一個(gè)(false)或上一個(gè)(true)鏈接。
默認(rèn)值: true
$size
(string|int[]) (可選) 圖像的大小。接受任何注冊(cè)的圖像大小名稱,或以像素為單位的寬度和高度值數(shù)組(按此順序)。
默認(rèn)值: 'thumbnail'
$text
(bool) (可選) 鏈接文本。
默認(rèn)值: false
來源:
文件: 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 ( (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'.
*
* @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.
*/
echo apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text );
}