正则表达式更改 WordPress 过滤器中的 img 属性
我有一个为摄影师客户开发的自定义主题,需要实现图像的延迟加载,以便博客加载速度更快,因为他当前拥有的图像量导致博客加载速度非常慢,即使只显示五个帖子。为此,我使用 JAIL jquery 插件,但我需要能够修改图像标签才能正常工作。基本上,我必须用占位符替换 src 属性,并将 data-href 属性设置为源网址。我似乎找不到在 WordPress 过滤器内正常工作的解决方案,我基本上是在帖子中过滤 the_content() 挂钩..有谁知道我如何实现这一点?
I have a custom theme I've developed for a photographer client and need to implement lazy-loading of the images so that the blog loads faster as it is horribly slow due to the amount of images he currently has, even when only showing five posts. To do this I'm using the JAIL jquery plugin but I need to be able to modify the image tags for it to work properly.. basically I have to replace the src attribute with a placeholder and set a data-href attribute to the source url. I cannot seem to find a resolution that works properly inside of a wordpress filter, I'm basically filtering the_content() hook in the posts.. does anyone know how I could accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于这些问题,Stackoverflow 的标准陈词滥调是您应该使用 DOM 解析器。这实际上是正确的,但对于输出操作来说不太可行(性能)。
为了实现你想要的,你可以尝试:
然后定义一个像这样的回调:
请注意,只有当你的所有图像链接一致地遵循这个方案时,这个正则表达式才有效(例如,它们都应该使用双引号)。
The standard Stackoverflow cliche for these questions is that you should use a DOM parser. Which is actually correct, but not quite feasible (performance) for output manipulation.
To accomplish what you want you could try:
Then define a callback like this:
Note that this regex is only workable if all your image links follow this scheme consistently (they all should be using double quotes for example).