WordPress开发函数adjacent_image_link()
WordPress开发函数adjacent_image_link(),显示下一个或上一个图像链接,该链接具有相…
WordPress开发函数adjacent_image_link(),显示下一个或上一个图像链接,该链接具有相同的文章父。
用法:
adjacent_image_link( bool $prev = true, string|int[] $size = ‘thumbnail’, bool $text = false )
描述:
从$post全局变量中检索当前附件对象。
参数:
$prev
(bool) (可选) 是否显示下一个(false)或上一个(true)链接。
默认值: true
$size
(string|int[]) (可选) 图像的大小。接受任何注册的图像大小名称,或以像素为单位的宽度和高度值数组(按此顺序)。
默认值: ‘thumbnail’
$text
(bool) (可选) 链接文本。
默认值: 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 );
}
更新日志:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!