【问题标题】:Adding image alt attribute field添加图像 alt 属性字段
【发布时间】:2017-02-27 23:45:23
【问题描述】:

我有这些代码:

$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 行

【问题讨论】:

  • 所以你想将图像文件名作为 alt 属性传递?
  • 是的,尝试这样 $first_img = "";返回$first_img; }
  • 我猜$post_image_id是图片的名字?
  • 谢谢@RyanCyrus

标签: php wordpress image function alt


【解决方案1】:

在你的函数中传递 $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>';

【讨论】:

  • 谢谢伙计,太接近了,但结果是;相同的替代文字,每张图片,我想使用不同的文字每张图片,如图片标题或(我将图片上传到 wp 画廊,每张图片都有 alt attri。)画廊 alt attr。如果可能的话..有结果截图link
  • @DmN 显然是好伙伴。我只是向你展示了一种方法。您没有向我们展示您的循环/如何获取滑块图像。如果您从 db 获取滑块图像,则在此处添加一个 image_alt 列,然后将 $this-&gt;get_post_image($post-&gt;ID,$image_size, "Alt text") 中的 "Alt text" 更改为 $post-&gt;image_alt 之类的东西,然后您的 alt 对于图像会有所不同。如果您需要帮助,请告诉我
  • 谢谢你的朋友,我是 php 新手,由于我的英语,我无法清楚地解释我的问题。我现在就这么做,谢谢。使用.$this-&gt;get_post_image($post-&gt;ID,$image_size,$post-&gt;post_tit‌​le).'&lt;/a&gt;&lt;/span&gt;'; 并将$img_alt 更改为$post_title 即可。页面滑块底部有php example 的结果,再次感谢。
  • 真的很高兴你能弄明白
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
  • 2016-12-24
  • 2022-01-09
  • 1970-01-01
相关资源
最近更新 更多