我正在使用 Wordpress 的多帖子缩略图插件,但无法找到一种方法来删除 WordPress 似乎生成的默认 html 属性?当您查看它提供的 html 源代码时;
<img width="600" height="450" src="http://localhost/news/../media/news-specials/hotplate2thumb.jpg" class="attachment-post-thumbnail size-post-thumbnail" alt="hotplate2thumb" />
我想删除宽度和高度,因为这是无效代码。我也想删除该类,以使网站看起来不那么按字。
理想情况下,它应该生成为:
<img src="http://localhost/news/../media/news-specials/hotplate2thumb.jpg" alt="hotplate2thumb" />
我已经在函数中有了这个.php
//remove class from the_post_thumbnail
function the_post_thumbnail_remove_class($output) {
$output = preg_replace('/class=".*?"/', '', $output);
return $output;
}
add_filter('post_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );
function remove_width_attribute( $html ) {
$html = preg_replace( '/(width|height)="'d*"'s/', "", $html );
return $html;
}
但这仅适用于 wordpress 的 defult 帖子缩略图,而不适用于多个/次要缩略图。
我还尝试在模板上实现这样的代码:
<?php
if (class_exists('MultiPostThumbnails')):
echo "<img src='"".MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'secondary-image') . "'" alt='"'" />";
endif; ?>
但是即使我没有一组,这仍然为所有帖子渲染了第二张图像,因此我会在主题中出现损坏的图像。并非所有我的帖子都使用第二张图片,只有少数。
谢谢
好的,
我通过添加解决了它
add_filter('post_secondary-image_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter( 'post_secondary-image_thumbnail_html', 'remove_width_attribute', 10 );
到这个函数
//remove class from the_post_thumbnail
function the_post_thumbnail_remove_class($output) {
$output = preg_replace('/class=".*?"/', '', $output);
return $output;
}
add_filter('post_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter('post_secondary-image_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter( 'post_secondary-image_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );
function remove_width_attribute( $html ) {
$html = preg_replace( '/(width|height)="'d*"'s/', "", $html );
return $html;
}