我想方设法想办法解决这个问题。我想从以下位置更改WordPress中上载图像的标记:

<div id="attachment_906" style="width: 590px" class="wp-caption aligncenter">
    <img class="size-full wp-image-906  " title="Image Alignment 580x300" alt="Image Alignment 580x300" src="http://localhost/sandbox/wp-content/uploads/2013/03/image-alignment-580x300.jpg" width="580" height="300">
    <p class="wp-caption-text">Look at 580×300 getting some <a title="Image Settings" href="http://en.support.wordpress.com/images/image-settings/">caption</a> love.</p>
</div>

更像这样:
<figure class="center">
    <img class="gallery-img" src="http://lorempixel.com/300/500/">
    <figcaption>
        <span>A slightly longer caption for the image, which is significantly smaller than the last one. pretty neat!</span>
    </figcaption>
</figure>

我知道原始帖子中的大部分标记都需要保留,但是有什么方法可以自定义这些标记吗?即使我可以得到相同的基本结构(包装器、图片、标题包装器、标题),我也可以用CSS从那里获取它。
因为我正在开发一个主题,所以我需要使用本地WordPress工具的选项。如果不是这样的话,我就不能工作了。
我原本希望有一个过滤器或行动,我可以申请这样做,但我没有运气找到这样的事情。
提前谢谢!

最佳答案

你需要使用image_send_to_editor过滤器。https://developer.wordpress.org/reference/hooks/image_send_to_editor/
在任何一个活动主题或插件的php文件(如functions.php)中都可能出现类似的情况;

function filter_image_send_to_editor($content) {
  $content .= '<figure class="center">(Generate your markup here)</figure>';

  return $content;
}
add_filter('image_send_to_editor', 'filter_image_send_to_editor');

当然,您必须在该函数中编写自己的代码。

关于php - 如何自定义插入WordPress帖子中的图像的HTML标记?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34876254/

10-12 16:22
查看更多