doushuo1080 2018-01-03 02:25
浏览 30
已采纳

如何删除AMP页面中的第一个图像

So I have code in place to remove the first image of all posts (this is because I put an image at the top of every post before featured images were introduced, and once featured images were created, I just put code in place to remove the first image so I wouldn't have to go back and do it manually). I'm trying to get it work for AMP pages, but haven't been able to figure it out yet.

This is the function for the normal pages.

 function remove_first_image ($content) {
    if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
    $content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
    }   
    return $content;
    }
    add_filter('the_content', 'remove_first_image');

And since that doesn't remove the first image on AMP pages, I tried something like this which has some correct elements in it but isn't working.

add_action( 'pre_amp_render_post', function () {
    add_filter( 'the_content', function( $content ){
         $content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
         return $content;
    }, 3 );
});

Any thoughts on how to get this to work for AMP content?

  • 写回答

1条回答 默认 最新

  • dshkmamau65777662 2018-01-03 04:05
    关注

    I think the code example you listed above is to use with the AMP plugin from Automattic https://wordpress.org/plugins/amp/.

    Are you using a different AMP plugin... I saw reference to wp-amp-ninja. You might have to check their code to find the actions and filters. It may not be the same... and then you won't be positive they don't change this in updates, if it's not properly documented.

    You can try switching to the Automattic one. Or if you're using the premium version of the wp-amp-ninja plugin, I see they offer CSS options in the upgrade.

    Like I mention, I think your code will work with the Automattic plugin. If you switch to it and out of interest, want to try remove the image with styles instead of the_content filter... That is, if the 2 images still appear... Use the amp_post_template_css action. Use the browser's inspect element to find the correct css class name. (might be: amp-img.wp-post-image)

    function wp_amp_additional_css_styles( $amp_template ) {
    ?>
      amp-img.wp-post-image {
            display: none;
      }
    
    <?php
    }
    add_action( 'amp_post_template_css', 'wp_amp_additional_css_styles' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)