因此,我想修改默认在帖子正文中显示图像的方式。我有一个可以正常工作的简单函数:
function post_img_unwrap($post, $query)
{
$img_regex = '/(<img.*?>)/s';
$img_template = '<div class="post-img centered">$1</div>';
$post->post_content = apply_filters('the_content', $post->post_content);
$post->post_content = preg_replace($img_regex, $img_template, $post->post_content);
return $post;
}
add_filter('the_post', 'post_img_unwrap', 10, 2);
问题是我得到了重复的div,如下所示:
<div class="post-img centered">
<div class="post-img centered">
<img>
</div>
</div>
显然,问题是,我在做什么错了?