2011-11-19 73 views
1

我試圖將圖像標題添加到圖庫中alt屬性的圖像,但我的代碼不起作用。下面是修改爲實施幻燈片的圖庫簡碼的一部分。在底部,我使用wp_get_attachment_image()$default_attr作爲包含標題的屬性數組。標題不顯示在HTML中。wordpress獲取圖像標題與wp_get_attachment_image()

$id = intval($id); 
    if ('RAND' == $order) 
     $orderby = 'none'; 

    if (!empty($include)) { 
     $include = preg_replace('/[^0-9,]+/', '', $include); 
     $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby)); 

     $attachments = array(); 
     foreach ($_attachments as $key => $val) { 
      $attachments[$val->ID] = $_attachments[$key]; 
     } 
    } elseif (!empty($exclude)) { 
     $exclude = preg_replace('/[^0-9,]+/', '', $exclude); 
     $attachments = get_children(array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby)); 
    } else { 
     $attachments = get_children(array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby)); 
    } 

    if (empty($attachments)) 
     return ''; 

    if (is_feed()) { 
     $output = "\n"; 
     foreach ($attachments as $att_id => $attachment) 
      $output .= wp_get_attachment_link($att_id, $size, true) . "\n"; 
     return $output; 
    } 
    $i = 0; 


    $default_attr = array(
    'src' => $src, 
    'class' => "attachment-$size", 
    'alt' => trim(strip_tags($attachment->post_excerpt)) 
    ); 

foreach ($attachments as $attachment) { 
    <a href='".wp_get_attachment_url($attachment->ID)."'>".wp_get_attachment_image($attachment->ID, $size, false, $default_attr)."</a>  
}  

return $output; 

回答

0

您可以使用:

wp_get_attachment_url($attachment->post_title); 
1

來到過這一點的同時尋找相同的答案,找到它,並希望分享它:

$attachment=get_post($attachment_id); 
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true); 
$image_title = $attachment->post_title; 
$caption = $attachment->post_excerpt; 
$description = $image->post_content; 

輸出你的標題由WordPress格式化(添加休息和段落),你可以使用:

$caption = apply_filters('the_content', $caption);