post_thumbnail_html
要更改html输出,需要挂接过滤器
post_thumbnail_html
From Codex:
apply_filters( \'post_thumbnail_html\', string $html, int $post_id,
string $post_thumbnail_id, string|array $size, string $attr )
过滤帖子缩略图HTML。
此筛选器由调用get_the_post_thumbnail
位于post\\u thumbnail\\u template的第177行。php之后$html
已设置为:
$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
在上下文中筛选:
/**
* Filters the post thumbnail HTML.
* @param string $html The post thumbnail HTML.
* @param int $post_id The post ID.
* @param string $post_thumbnail_id The post thumbnail ID.
* @param string|array $size The post thumbnail size. Image size or array of width and height values (in that order). Default \'post-thumbnail\'.
* @param string $attr Query string of attributes.
*/
return
apply_filters( \'post_thumbnail_html\', $html, $post->ID, $post_thumbnail_id, $size, $attr );
添加data-featherlight="mylightbox"
示例:
因此添加
data-featherlight="mylightbox"
可能是这样的:
add_filter(\'post_thumbnail_html\', \'my_thumbnail_filter_method\', 10, 5 );
function my_thumbnail_filter_method($html, $post->ID, $post_thumbnail_id, $size, $attr) {
$id = get_post_thumbnail_id();
$src = wp_get_attachment_image_src($id, $size);
$alt = get_the_title($id); /
$class = $attr[\'class\'];
$html = \'<img src="\' . $src[0] . \'" alt="\' . $alt . \'" class="\' . $class . \'" data-featherlight="mylightbox" />\';
}
另两个行动挂钩位于
$html
设置在
get_the_pos_thumbnail
.
begin_fetch_post_thumbnail_html
end_fetch_post_thumbnail_html
但两人都只是过路$post->ID, $post_thumbnail_id, $size
. 看起来像是过滤post_thumbnail_html
是访问<img>
html字符串。上的注释has_post_thumbnail()
, 因为它似乎经常出现:如果有任何图像附加到帖子,此方法将返回true,而不是专门针对帖子缩略图(又名特色图像)。