Jeremy提供的指向我的原始博客的链接对于运行3.2或之前版本的WordPress站点很有用(不确定是否运行3.3,尚未测试),但我最近注意到它不适用于3.4。我还没有机会更新或写一篇关于它的新帖子,但这里有一个修改过的函数可以处理它:
add_shortcode(\'wp_caption\', \'fixed_img_caption_shortcode\');
add_shortcode(\'caption\', \'fixed_img_caption_shortcode\');
function fixed_img_caption_shortcode($attr, $content = null) {
if ( ! isset( $attr[\'caption\'] ) ) {
if ( preg_match( \'#((?:<a [^>]+>\\s*)?<img [^>]+>(?:\\s*</a>)?)(.*)#is\', $content, $matches ) ) {
$content = $matches[1];
$attr[\'caption\'] = trim( $matches[2] );
}
}
$output = apply_filters(\'img_caption_shortcode\', \'\', $attr, $content);
if ( $output != \'\' )
return $output;
extract(shortcode_atts(array(
\'id\' => \'\',
\'align\' => \'alignnone\',
\'width\' => \'\',
\'caption\' => \'\'
), $attr));
if ( 1 > (int) $width || empty($caption) )
return $content;
if ( $id ) $id = \'id="\' . esc_attr($id) . \'" \';
return \'<div \' . $id . \'class="wp-caption \' . esc_attr($align) . \'" style="width: \' . $width . \'px">\'
. do_shortcode( $content ) . \'<p class="wp-caption-text">\' . $caption . \'</p></div>\';
}