admin管理员组文章数量:1318563
I am using Automattic's plugin to implement AMP on my website. Currently, to make my videos responsive, I wrap YouTube videos around a custom div. So, a YouTube video on my website looks like this:-
<div class="yt">
<iframe>....YT video code...</iframe>
</div>
The problem is the <div class="yt">
and the closing </div>
both appear on the AMP pages too. How can I remove them? Already tried asking on the official support. No replies yet.
I am using Automattic's plugin to implement AMP on my website. Currently, to make my videos responsive, I wrap YouTube videos around a custom div. So, a YouTube video on my website looks like this:-
<div class="yt">
<iframe>....YT video code...</iframe>
</div>
The problem is the <div class="yt">
and the closing </div>
both appear on the AMP pages too. How can I remove them? Already tried asking on the official support. No replies yet.
- How are you adding them? With code? Or just in the editor? – Jacob Peattie Commented Nov 5, 2018 at 15:43
- If you are asking about the custom div, I add them via the editor. So, when I have to add a video, I go to the HTML Editor for the post and add the video embed code within the custom yt div – user1928108 Commented Nov 5, 2018 at 15:46
2 Answers
Reset to default 1Thanks to Thomas' answer to my question at StackOverflow - https://stackoverflow/questions/53158606/remove-div-with-a-class-without-removing-content
Here's how you can do it on Wordpress for AMP. In your theme's functions.php
add the following:-
add_action( 'pre_amp_render_post', 'amp_vid_rmv' );
function amp_vid_rmv() {
add_filter( 'the_content', 'amp_vid_div_rmv' );
}
function amp_vid_div_rmv($content) {
$content = preg_replace('#<div class="yt">([\s\S]*?)</div>#', '$1', $content);
return $content;
}
Please feel free to improve on this.
Sometimes you want to delete not the html code but shortcode. Then use something like that:
add_action( 'pre_amp_render_post', 'amp_vid_rmv' );
function amp_vid_rmv() {
add_filter( 'the_content', 'amp_vid_div_rmv' );
}
function amp_vid_div_rmv($content) {
$content = preg_replace('#\[shortcode_name_here\]<br \/>#', '$1', $content);
?> <div>in func 9 </div> <?php
return $content;
}
本文标签: phpRemove Custom Divs from AMP pages
版权声明:本文标题:php - Remove Custom Divs from AMP pages 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742048605a2417939.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论