我在上传图像时给alt标记,但它不是接受给定的图像alt标记,它显示为空白。
我试图添加这个过滤器,但仍然没有工作。
/* Register callback function for post_thumbnail_html filter hook */
add_filter( 'post_thumbnail_html', 'meks_post_thumbnail_alt_change', 10, 5 );
/* Function which will replace alt atribute to post title */
function meks_post_thumbnail_alt_change( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
$post_title = get_the_title();
$html = preg_replace( '/(alt=")(.*?)(")/i', '$1'.esc_attr( $post_title ).'$3', $html );
return $html;
}post内容显示在loop-single.php文件中。
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->如何在贴图中使用alt标签
发布于 2016-12-15 09:00:30
嗯,我发现了错误,似乎前面的开发人员手动添加了图像到帖子中,并且还设置了功能图像。所以我搞糊涂了。
我只是将alt文本直接放在post的编辑模式中。
发布于 2016-12-15 06:09:32
试试看,它对我有用:)
function change_post_thumbnail_html($html, $post_id, $post_thumbnail_id, $size, $attr) {
$id = get_post_thumbnail_id();
$src = wp_get_attachment_image_src($id, $size);
$alt = get_the_title($id); // gets the post thumbnail title
$class = $attr['class'];
$html = '<img src="' . $src[0] . '" alt="' . $alt . '" class="' . $class . '" />';
return $html;
}
add_filter('post_thumbnail_html', 'change_post_thumbnail_html', 99, 5);https://stackoverflow.com/questions/41157103
复制相似问题