如何总是为图片添加标题元素--即使是空的?

时间:2015-10-17 作者:Henning Fischer

我希望将标题始终添加到图像中,即使它是空的。这是我添加标题的函数,但标题元素中没有内容时不会生成,如何始终添加标题元素?

add_filter(\'img_caption_shortcode\', \'caption_shortcode_inline_text\', 10, 3);
function caption_shortcode_inline_text($empty, $attr, $content) {
extract(shortcode_atts(array(
    \'id\'    => \'\',
    \'align\' => \'alignnone\',
    \'width\' => \'\',
    \'caption\' => \'\'
), $attr));

// in this part I amtrying to always add the caption element but it doesn\'t work:

if (1 < (int) $width || 1 > (int) $width || $caption || !$caption)
    return \'<div \' . $id . \'class="wp-caption \' . esc_attr($align) . \'" style="width: \' . (10 + (int) $width) . \'px">\'
    . do_shortcode($content) . \'<p class="wp-caption-text">\' . $caption . \'</p></div>\';
}

1 个回复
SO网友:Henning Fischer

This works:

add_filter( \'disable_captions\', create_function(\'$a\', \'return true;\') );
function image_send_to_editor_2($html, $id, $caption, $title, $align, $url, $size, $alt) {

    $width = \'auto\';

    if ( preg_match( \'/width="([0-9]+)/\', $html, $matches ) ) {
        $width = $matches[1] . \'px\';
    }

    $output = \'<div id="attachment-\' . $id . \'" class="wp-caption align\' . $align . \'" style="width: \' . $width . \';">\';
    $output .= $html;
    $output .= \'<p class="wp-caption-text">\' . $caption . \'</p>\';
    $output .= \'</div>\';
    return $output;

}

add_filter(\'image_send_to_editor\', \'image_send_to_editor_2\', 10, 8);

相关推荐

Are captions stored anywhere?

关于我之前的question about shortcode captions, 在我看来,标题的实际文本并不是存储在短代码本身的帖子内容之外的任何地方。我会认为wp_get_attachment_metadata 将存储附件的信息,但它不会。我错了吗?或者WordPress没有在任何地方存储实际的标题?