添加图像alt属性字段

时间:2017-02-27 23:45:23

标签: php wordpress image function 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上的wordpress插件https://wordpress.org/plugins/carousel-horizontal-posts-content-slider/

第340行和第404-427行

1 个答案:

答案 0 :(得分:0)

在你的函数中传递$ img_alt。

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>';
相关问题