如何在Media.php中获取附加的图片Alt文本?

时间:2015-01-06 作者:Emil cavadov

首先,我为我的英语道歉。我想得到attached image alt text wp中的以下代码包括/介质。php

function img_caption_shortcode( $attr, $content = null ) {
    // New-style shortcode with the caption inside the shortcode with the link and image tags.
    if ( ! isset( $attr[\'caption\'] ) ) {
        if ( preg_match( \'#((?:<a [^>]+>\\s*)?<img [^>]+>(?:\\s*</a>)?)(.*)#is\', $content, $matches ) ) {
            $content = $matches[1];
            $attr[\'caption\'] = trim( $matches[2] );
        }
    }

    /**
    * Filter the default caption shortcode output.
    *
    * If the filtered output isn\'t empty, it will be used instead of generating
    * the default caption template.
    *
    * @since 2.6.0
    *
    * @see img_caption_shortcode()
    *
    * @param string $output  The caption output. Default empty.
    * @param array  $attr    Attributes of the caption shortcode.
    * @param string $content The image element, possibly wrapped in a hyperlink.
    */
    $output = apply_filters( \'img_caption_shortcode\', \'\', $attr, $content );
    if ( $output != \'\' )
        return $output;

    $atts = shortcode_atts( array(
        \'id\'      => \'\',
        \'align\'   => \'alignnone\',
        \'width\'   => \'\',
        \'caption\' => \'\',
        \'class\'   => \'\',
        \'alt\'   => \'\',
    ), $attr, \'caption\' );

    $atts[\'width\'] = (int) $atts[\'width\'];
    if ( $atts[\'width\'] < 1 || empty( $atts[\'caption\'] ) )
        return $content;

    if ( ! empty( $atts[\'id\'] ) )
        $atts[\'id\'] = \'id="\' . esc_attr( $atts[\'id\'] ) . \'" \';

    $class = trim( \'wp-caption \' . $atts[\'align\'] . \' \' . $atts[\'class\'] );

    if ( current_theme_supports( \'html5\', \'caption\' ) ) {
        return \'<figure \' . $atts[\'id\'] . \'style="width: \' . (int) $atts[\'width\'] . \'px;" class="\' . esc_attr( $class ) . \'">\'
        . do_shortcode( $content ) . \'<figcaption class="wp-caption-text">\' . $atts[\'caption\'] . \'</figcaption></figure>\';
    }

    $caption_width = 10 + $atts[\'width\'];

    /**
    * Filter the width of an image\'s caption.
    *
    * By default, the caption is 10 pixels greater than the width of the image,
    * to prevent post content from running up against a floated image.
    *
    * @since 3.7.0
    *
    * @see img_caption_shortcode()
    *
    * @param int    $caption_width Width of the caption in pixels. To remove this inline style,
    *                              return zero.
    * @param array  $atts          Attributes of the caption shortcode.
    * @param string $content       The image element, possibly wrapped in a hyperlink.
    */

    $caption_width = apply_filters( \'img_caption_shortcode_width\', $caption_width, $atts, $content );

    $style = \'\';
    if ( $caption_width )
        $style = \'style="width: \' . (int) $caption_width . \'px" \'; 

    return \'<h3>\' . $atts[\'caption\'] . \'</h3><div \' . $atts[\'id\'] . $style . \'class="\' . esc_attr( $class ) . \'">\'
    . do_shortcode( $content ) . \'<p class="wp-caption-text">\' . $alt_text . \'</p></div>\';
}
我能写些什么呢$alt_text 我获取附加图像alt文本的变量?

1 个回复
SO网友:flomei

在答案中this forum post 提到

$alt = get_post_meta($attachment_id, \'_wp_attachment_image_alt\', true);
会帮你完成工作的。也许你应该试试看,看看那里的更多信息。

结束

相关推荐

Images not rendering

我有一个烘焙博客,最近我更改了url。我现在的问题是,当我将图像添加到帖子或图书馆时,它们不会显示/呈现。上载步骤通过OK,但图像不会渲染。你能帮忙吗,我只是不知道我哪里做错了。