简体   繁体   English

如何在 WordPress 中检查页面是否通过 AMP 加载?

[英]How can I check if a page is loading via AMP in WordPress?

So in WordPress, there are a number of conditionals that allow you to run functions on certain parts of the site and not on others.因此,在 WordPress 中,有许多条件允许您在网站的某些部分而不是其他部分上运行功能。 For example:例如:

is_feed() will allow you to target RSS feeds.
is_singular() will allow you to target single posts and pages.
is_amp() will allow you to target pages being served via the AMP version.

Just kidding on that last one.开玩笑的最后一个。 That function doesn't exist, but that's what I'm asking about.该功能不存在,但这就是我要问的。 Is there a function or a mechanism that I can use to test if a page is being served via the AMP version of that page?是否有一种功能或机制可用于测试页面是否通过该页面的 AMP 版本提供服务?

My desired code would be something like this:我想要的代码是这样的:

if( true === is_singular() && !is_feed() && !is_amp() ):
    add_filter( 'the_content','swp_insert_pinterest_image', 10 );
endif;

Of course, is_amp() is too generic so I'll add my vendor prefixes and whatnot to avoid name collisions, but I digress.当然,is_amp() 太通用了,所以我会添加我的供应商前缀和诸如此类的东西以避免名称冲突,但我离题了。

So, how can I check for AMP in WordPress?那么,如何在 WordPress 中检查 AMP? Is there a function that we can write or perhaps there's a query parameter or $_GET variable that can be checked?是否有我们可以编写的函数,或者是否有可以检查的查询参数或 $_GET 变量?

To determine if you are currently on an AMP page, use is_amp_endpoint() .要确定您当前是否在 AMP 页面上,请使用is_amp_endpoint()

EDIT: is_amp_endpoint() is now deprecated.编辑: is_amp_endpoint()现在已弃用。 Use amp_is_request() as of v2.0.从 v2.0 开始使用amp_is_request()

只是为了更新,以上已更新为 amp_is_request()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM