如何包装< script>进入wordpress< head>的if语句 [英] How to wrap <script> into if statement for wordpress <head>
问题描述
我一直在尝试实现由wordpress插件制造商提供的代码,我需要一些帮助来确认它是否有效.有问题的插件是PaidMembership Pro,它为会员隐藏广告提供了可能性.这是我应该添加到模板中的代码片段:
I've been trying to implement a code provided by a wordpress plugin maker and I need a bit of help confirming if it works or not. The plugin in question is the PaidMembership Pro and it provides a possibility to hide ads for members. This is the snippet of code I should add to the template:
if ( function_exists( 'pmpro_displayAds' ) && pmpro_displayAds() ) {
//insert ad code here
}
广告代码是一个简单的Google adsense代码:
The ad code is a simply google adsense code:
<script data-ad-client="ca-pub-111111111111111" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
我不知道该怎么做.我尝试将它像这样添加到wordpress header.php中,但是wordpress抛出一个错误.
I have no idea how to do it right. I tried adding it like this to wordpress header.php but wordpress throws me an error.
<?php if ( function_exists( 'pmpro_displayAds' ) && pmpro_displayAds() ) {
<script data-ad-client="ca-pub-111111111111111" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
} ?>
我也尝试了其他几种变体,但似乎没有用.有人可以帮我至少将我指向可以寻找答案的方向吗?if应该如何包装或应该完全包装?如果我根本不包装它,它只会显示在前端的顶部.
I tried a couple of other variations too but didn't seem to work. Could someone help me out at least pointing me to a direction where I can look for answers? How the if should be wrapped or should it be at all? If I don't wrap it at all, it just shows up on the top of the frontend.
谢谢
推荐答案
您在尝试中混合使用了HTML和PHP代码.要使PHP生成所需的HTML,您必须将其回显:
You have mixed HTML and PHP code in your try. To make PHP generate the HTML you wish, you have to echo it:
<?php
if ( function_exists( 'pmpro_displayAds' ) && pmpro_displayAds() ) {
echo('<script data-ad-client="ca-pub-111111111111111" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>');
}
?>
有关此的更多信息,请参见 echo()
在PHP手册中
for more on this, see echo()
in PHP-Manuals
所有PHP代码(例如wordpress-logic)必须包装在<?php?>
中,以便由网络服务器执行.
All PHP-Code (e.g. wordpress-logic) has to be wrapped in <?php ?>
in order to get executed by the webserver.
这篇关于如何包装< script>进入wordpress< head>的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!