使用GET_IMAGE_TAG滤镜的正确方式是什么?

时间:2011-06-02 作者:Travis Northcutt

我正在尝试删除帖子缩略图的title属性(在某些帖子上),我假设get\\u image\\u标记过滤器就是这样做的。然而,到目前为止,我所得到的并不奏效。我需要做什么改变才能使这项工作正常进行?我的代码:

add_filter(\'get_image_tag\', \'image_no_title\');
function image_no_title($title) {
    $title = \'\';
    return $title;
}
以及相关函数(来自wp includes/media.php):

function get_image_tag($id, $alt, $title, $align, $size=\'medium\') {

    list( $img_src, $width, $height ) = image_downsize($id, $size);
    $hwstring = image_hwstring($width, $height);

    $class = \'align\' . esc_attr($align) .\' size-\' . esc_attr($size) . \' wp-image-\' . $id;
    $class = apply_filters(\'get_image_tag_class\', $class, $id, $align, $size);

    $html = \'<img src="\' . esc_attr($img_src) . \'" alt="\' . esc_attr($alt) . \'" title="\' . esc_attr($title).\'" \'.$hwstring.\'class="\'.$class.\'" />\';

    $html = apply_filters( \'get_image_tag\', $html, $id, $alt, $title, $align, $size );

    return $html;
}

3 个回复
最合适的回答,由SO网友:Travis Northcutt 整理而成

多亏了拉斯特指出我错了地方,我做了更多的挖掘,最终得出以下结论:

add_filter(\'wp_get_attachment_image_attributes\', \'wpse_19029_no_image_title\');

function wpse_19029_no_image_title ($attr) 
{
    unset($attr[\'title\']);
    return $attr;
}

SO网友:Rarst

过滤器始终影响第一个参数,其余参数用于获取其他信息,默认情况下甚至不会传递。

add_filter(\'get_image_tag\', \'image_no_title\', 10, 4);

function image_no_title( $html, $id, $alt, $title ) {

    return str_replace("title=\\"{$title}\\"", \'\', $html);
}

SO网友:benna

我遇到了同样的问题,我需要在现有的post image类属性中附加一个类。根据您的示例,我的代码是正确的,但无法理解为什么类没有更新。

我所做的是在操作和过滤器准备就绪后,将媒体库中的图像重新插入帖子/页面(用于特色图像和自定义字段)。

这其实很符合逻辑。。。我意识到这篇文章存储在数据库中,然后被提取出来,为网站的“前端”生成html,但没有对数据库条目进行任何更改,因此我不能期望任何更改立即在网站上可见!

结束

相关推荐

Images don't show up

我刚刚安装了一个新的多站点安装。一切都像我一样工作,只是图像没有显示在任何子网站上。Any idea why the images don\'t show up?我正在共享Windows服务器(由Arvixe.com托管)中的IIS6上运行Wordpress 3.1.1。辅助站点位于子目录而不是子域中。主站点的图像与标题图像一样有效,但子站点中的图像不会显示。我知道这些图片在博客中出现时,正在正确上传。服务器上的dir文件夹。如果我这样导航它们,它们也会出现:http://www.example.com/