Wordpress wp-post-image class remover

2019-07-04 02:55发布

We use the_post_thumbnails to get our images displayed.

We want to use them with a specific class called "test" but we cant manage it!

We use this code:

 <?php the_post_thumbnail('pin-photo-large', array('class' => 'test')); ?>

But the output of the image seems like this:

 class="test wp-post-image"

How can we overcome this issue and get an output like this ?

 class="test" 

标签: wordpress
2条回答
女痞
2楼-- · 2019-07-04 03:28

Add this to your functions.php file :

function remove_post_image_class($content) {
    $post_string = $content;
    $post_string = preg_replace('/<img class=".*?"/', '<img', $post_string);
    return $post_string;
}

add_filter( 'the_content', 'remove_post_image_class' );

Enjoy

查看更多
趁早两清
3楼-- · 2019-07-04 03:43

Update: Add this to your functions.php file:

remove_action( 'begin_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_add' );

Explanation: In media.php, the _wp_post_thumbnail_class_filter function adds the wp-post-image class to post thumbnails. By removing the begin_fetch_post_thumbnail_html action hook (located in default-filters.php), the function will no longer apply to the_post_thumbnail.


Old answer below:

I also searched for a proper way to filter out the wp-post-image class, alas to no avail. The only solution I figured out was to edit a (gasp!) core file: media.php and replace this:

function _wp_post_thumbnail_class_filter( $attr ) {
    $attr['class'] .= ' wp-post-image';
    return $attr;
}

by this:

function _wp_post_thumbnail_class_filter( $attr ) {
    return $attr;
}

There's certainly a better way to do this than to patch a core file, but this can be a good temporary solution.

查看更多
登录 后发表回答