不显示Get_the_Post_Thumb()标题和alt属性

时间:2020-01-29 作者:Mark Bosky

我正在尝试显示\'alt\'\'title\' 后期缩略图图像上的属性,但实际显示为已传递的唯一属性是\'class\'. \'缺少“alt”和“title”。你知道原因是什么吗?

$thumbnail_id = get_post_thumbnail_id(  );
$image_alt = get_post_meta($thumbnail_id, \'_wp_attachment_image_alt\', true);
echo $image_alt; //correct alt attribute text
echo获取\\u post\\u缩略图($post->ID,\'post thumbnail\',[\'class=>\'classTest\')//作品

echo获取\\u post\\u缩略图($post->ID,\'post thumbnail\',[\'alt=>\'altTest\')//不起作用

echo获取\\u post\\u缩略图($post->ID,\'post thumbnail\',[\'title=>\'titleTest\'])//不起作用


  1. <img width="940" height="150" src="https:/...imgsrc.jpg" class="classTest attachment-post-thumbnail size-post-thumbnail wp-post-image"> 已添加classTest

  2. <img width="940" height="150" src="https:/...imgsrc.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image"> altTest缺失

  3. <img width="940" height="150" src="https:/...imgsrc.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image"> 缺少标题测试

1 个回复
SO网友:Mark Bosky

结果表明,这是由函数中的以下自定义主题过滤器引起的。php。注释已修复该问题。

function image_alt_tags($content) {
    global $post;
    preg_match_all(\'/<img (.*?)\\/>/\', $content, $images);
    if (!is_null($images)) {
        foreach($images[1] as $index => $value) {
            if (!preg_match(\'/alt=/\', $value)) {
                $new_img = str_replace(\'<img\', \'<img alt="\' . get_the_title() . \'"\', $images[0][$index]);
                $content = str_replace($images[0][$index], $new_img, $content);
            }
        }
    }
    return $content;
}
add_filter(\'the_content\', \'image_alt_tags\', 99999);

add_filter(\'post_thumbnail_html\', \'thumbnail_filter\', 99, 5);

function thumbnail_filter($html, $post_id, $post_thumbnail_id, $size, $attr) {
   // you can alter the resulted HTML here
   $html = preg_replace(array(\'/alt=\\".*?\\"/\', \'/title=\\".*?\\"/\'), \'\', $html);
   return $html;
}

相关推荐

如何在缩略图数组的‘alt’上使用Get_the_Title文本?

我当前正在使用<?php the_post_thumbnail(\'250px\', array(\'class\'=>\"review-siteshot\", \'alt\' => get_the_title() )); ?>我知道\'alt\' => \"review\" 将审查输出为所有文本。我想用get_the_title() 再加上“复习”让我title-text review 作为缩略图的alt文本。