使用短代码处理程序img_caption_shortcode
要为您呈现HTML,尽管您确实需要传递一个宽度(我们可以很容易地获得这个宽度wp_get_attachement_image_src
:
function wpse_138126_thumbnail_caption( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
if ( $post = get_post( $post_thumbnail_id ) ) {
if ( $size = wp_get_attachment_image_src( $post->ID, $size ) )
$width = $size[1];
else
$width = 0;
$html = img_caption_shortcode(
array(
\'caption\' => trim( "$post->post_excerpt $post->post_content" ),
\'align\' => \'alignright\',
\'width\' => $width,
),
$html
);
}
return $html;
}
add_filter( \'post_thumbnail_html\', \'wpse_138126_thumbnail_caption\', 10, 5 );