Wordpress自定义类到post_thumbnail

时间:2016-02-04 07:38:13

标签: wordpress wordpress-theming

我在Wordpress主题中使用以下内容。

<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    the_post_thumbnail( 'full', array( 'alt'  => 'banner' , 'claass'  => 'header-img')); // show featured image
} 
?>

它会添加所有属性,但是,当我将claass更改为class以设置自定义类时,它不会显示。

  

在更正课程之前

<img claass="header-img" alt="banner" src="http://domain.com/wp-content/uploads/2015/12/test-image.jpg">
  

纠正课后

<img alt="banner" src="http://domain.com/wp-content/uploads/2015/12/test-image.jpg">

我不知道为什么而且我很难过。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您需要在当前主题的functions.php文件中编写以下代码,

add_filter('post_thumbnail_html','add_class_to_thumbnail');
function add_class_to_thumbnail($thumb) {
    $thumb = str_replace('attachment-', 'header-img attachment-', $thumb);
    return $thumb;
}

注意:我还没有对上面的代码进行过测试,这是一个使用post_thumbnail_html过滤器的想法。

相关问题