首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取元属性(title,alt,...)wordpress图库图片?

如何获取元属性(title,alt,...)wordpress图库图片?
EN

Stack Overflow用户
提问于 2017-04-17 16:37:18
回答 1查看 2.4K关注 0票数 1

我已经使用此函数将默认的wordpress条样主题格式化为我的引导程序主题,并且运行良好。但是当我调用"image_alt“和"image_title”都没有成功。

有什么需要帮忙的吗?谢谢!

代码语言:javascript
复制
add_filter( 'post_gallery', 'bootstrap_gallery', 10, 3 );

function bootstrap_gallery( $output = '', $atts, $instance )
{
    $atts = array_merge(array('columns' => 3), $atts);

    $columns = $atts['columns'];
    $images = explode(',', $atts['ids']);

    $col_class = 'img-box col-sm-4 col-xs-6 col-xxs-12 no-padding wow flipInX animated'; // default 3 columns
    if ($columns == 1) { $col_class = 'img-box col-xs-12 no-padding wow flipInX animated';}
    else if ($columns == 2) { $col_class = 'img-box col-xs-6 col-xxs-12 no-padding wow flipInX animated'; }
    // other column counts

    $return = '<div class="row gallery">';

    $i = 0;

    foreach ($images as $key => $value) {

        if ($i%$columns == 0 && $i > 0) {
            $return .= '</div><div class="row gallery portfolio center">';
        }

        $image_attributes = wp_get_attachment_image_src($value, 'full');
        $image_alt = get_post_meta( $image->id, '_wp_attachment_image_alt', true);
        $image_title = $attachment->post_title;

        $return .= '
            <div class="'.$col_class.' img-box col-sm-4 col-xs-12 no-padding wow flipInX animated">
                <a data-gallery="gallery" href="'.$image_attributes[0].'">
                    <img src="'.$image_attributes[0].'" alt="'.$image_alt.'" class="img-responsive">
                </a>
                <div class="title">'.$image_title.'</div>
            </div>';

        $i++;
    }

    $return .= '</div>';

    return $return;
}
EN

回答 1

Stack Overflow用户

发布于 2017-04-17 19:16:37

先拿到镜像ID,试试这个

代码语言:javascript
复制
$image_id = get_post_thumbnail_id(get_the_ID());
or
$image_id = get_post($id);

然后你可以传递id来获取图片alt和图片标题。

代码语言:javascript
复制
$image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true);
$image_title = $image_id->post_title;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43448011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档