Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何获取wordpress gallery图像的元属性(标题、alt等)?_Php_Wordpress_Attributes_Gallery - Fatal编程技术网

Php 如何获取wordpress gallery图像的元属性(标题、alt等)?

Php 如何获取wordpress gallery图像的元属性(标题、alt等)?,php,wordpress,attributes,gallery,Php,Wordpress,Attributes,Gallery,我用这个函数将默认wordpress galley主题格式化为我的引导主题,效果很好。但是当我调用“image\u alt”和“image\u title”时没有成功 需要帮忙吗?谢谢大家! add_filter( 'post_gallery', 'bootstrap_gallery', 10, 3 ); function bootstrap_gallery( $output = '', $atts, $instance ) { $atts = array_merge(array('c

我用这个函数将默认wordpress galley主题格式化为我的引导主题,效果很好。但是当我调用“image\u alt”和“image\u title”时没有成功

需要帮忙吗?谢谢大家!

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;
}
add_filter('post_gallery','bootstrap_gallery',10,3);
函数引导库($output='',$atts,$instance)
{
$atts=array\u merge(数组('columns'=>3),$atts);
$columns=$atts['columns'];
$images=explode(“,”,$atts['id']);
$col_class='img box col-sm-4 col-xs-6 col-xxs-12无填充wow flipInX动画';//默认3列
如果($columns==1){$col_class='img box col-xs-12无填充wow flipInX animated';}
如果($columns==2){$col_class='img box col-xs-6 col-xxs-12无填充wow flipInX动画';}
//其他列计数
$return='';
$i=0;
foreach($key=>$value的图像){
如果($i%$columns==0&&$i>0){
$return.='';
}
$image_attributes=wp_get_attachment_image_src($value,'full');
$image\u alt=get\u post\u meta($image->id,'.\u wp\u attachment\u image\u alt',true);
$image\u title=$attachment->post\u title;
$return.='
“.$image_title”
';
$i++;
}
$return.='';
return$return;
}

首先获取图像ID,然后重试此操作

$image_id = get_post_thumbnail_id(get_the_ID());
or
$image_id = get_post($id);
然后您可以传递id以获取图像alt和图像标题

$image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true);
$image_title = $image_id->post_title;