2017-04-17 4 views
1

私はこの機能を使って、デフォルトのwordpress galleyテーマを自分のブートストラップテーマにフォーマットし、うまく動作します。しかし、私が "image_alt"と "image_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; 
} 

答えて

1

は、第一の画像IDを取得し、この

$image_id = get_post_thumbnail_id(get_the_ID()); 
or 
$image_id = get_post($id); 

を試してみて、あなたがイメージALTおよび画像のタイトルを取得するためのIDを渡すことができます。

$image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true); 
$image_title = $image_id->post_title; 
関連する問題