2017-02-27 56 views
0

我已經得到了這些代碼:添加圖片的alt屬性字段

$slider_gallery.= '<span class="chpcs_img"><a href="'.$post_link.'">'.$this->get_post_image($post->ID,$image_size).'</a></span>'; 
public function get_post_image($post_image_id, $img_size) { 

    if (has_post_thumbnail($post_image_id)): 
     $img_arr = wp_get_attachment_image_src(get_post_thumbnail_id($post_image_id), $img_size); $first_img = $img_arr[0]; 
    endif; 

    if (empty($first_img)) { 
     if (empty($this->options['settings']['default_image_url'])) { 
      $first_img = plugins_url('assets/images/default-image.jpg', __FILE__); 
     } 
     else { 
      $first_img = $this->options['settings']['default_image_url']; 
     } 
    } 
    $first_img = "<img src='".$first_img. "' />"; 
    return $first_img; 
} 

我想添加alt屬性,但無法弄清楚。通過對/plugins/carousel-horizo​​ntal-posts-content-slider/chpcs.php

線340線404-427

+0

所以,你想傳遞的圖像文件名作爲alt屬性? –

+0

是這樣想的$ first_img =「"try to getting image file name or getting image alt info from wp gallery"」; 返回$ first_img; } – DmN

+0

我認爲$ post_image_id是圖像的名稱? –

回答

0

通$ img_alt在功能WordPress插件https://wordpress.org/plugins/carousel-horizontal-posts-content-slider/的方式。

public function get_post_image($post_image_id, $img_size, $img_alt) { 

    if (has_post_thumbnail($post_image_id)): 
    $img_arr = wp_get_attachment_image_src(get_post_thumbnail_id($post_image_id), $img_size); $first_img = $img_arr[0]; 

    endif; 

    if(empty($first_img)) { 

    if(empty($this->options['settings']['default_image_url'])) { 

     $first_img = plugins_url('assets/images/default-image.jpg', __FILE__); 

    } else { 

     $first_img = $this->options['settings']['default_image_url']; 

    } 
} 
    $first_img = "<img src='". $first_img. "' alt='". $img_alt. "' />"; 
    return $first_img; 
} 

,然後您的電話線將

$slider_gallery.= '<span class="chpcs_img"><a href="'.$post_link.'">'.$this->get_post_image($post->ID,$image_size, "Alt text").'</a></span>'; 
+0

感謝隊友,太接近了,但結果是;相同的替代文字,每一個圖像,我想要使用不同的文字每個圖像像圖像標題或(我上傳圖像到WP畫廊和每個圖像都有替代。)gallery alt attr。如果可能..有結果截圖[鏈接](http://i.hizliresim.com/37NlBj.jpg) – DmN

+0

@DmN很明顯,交配。我只是告訴你一個辦法。你沒有向我們展示你的循環/你如何獲得滑塊圖像。如果你從db獲得你的滑塊圖像,那麼你在那裏添加一個'image_alt'列,然後改變'$ this-> get_post_image($ post-> ID,$ image_size,「Alt text」)中的''Alt text''。 '像'$ post-> image_alt'那麼你的ALT會對圖像有所不同。如果您需要幫助,請告知我 – JapanGuy

+0

感謝您配對,我是php新手,無法清楚地解釋我的問題,因爲我的英語。現在我會這樣做,謝謝你。使用'。$ this-> get_post_image($ post-> ID,$ image_size,$ post-> post_tit le)。'';'並將'$ img_alt'更改爲'$ post_title'。在頁面滑塊底部有結果[php示例](http://whatisthewik.com/php/),再次感謝。 – DmN