【问题标题】:If Else and Brackets Showing as Text in FooterIf Else 和括号在页脚中显示为文本
【发布时间】:2020-02-26 05:32:17
【问题描述】:

我在这里遗漏了什么导致 If、Else 和括号出现在页脚中?中间的所有脚本都根据需要运行,但由于某种原因,If、Else 和括号显示为文本。这张贴在我的 footer.php 中

if(user.loggedin?) {
    <script>
      window.intercomSettings = {
        app_id: 'abc123',
        custom_launcher_selector:'#intercom',
        name: "<?=wp_get_current_user()->user_firstname?>", // Full name
        email: "<?=wp_get_current_user()->user_email?>", // Email address
        created_at: "<?=strtotime(wp_get_current_user()->created_at)?>" // Signup date as a Unix timestamp
      };
    </script>
    <script>
    // We pre-filled your app ID in the widget URL: 'https://widget.intercom.io/widget/'
    (function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',w.intercomSettings);}else{var d=document;var i=function(){i.c(arguments);};i.q=[];i.c=function(args){i.q.push(args);};w.Intercom=i;var l=function(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/u9m06p0a';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);};if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})();
    </script>
} else {
    <script>
        window.intercomSettings = {
        app_id: 'abc123',
    	custom_launcher_selector:'#intercom'
};
    </script>
    <script>
    // We pre-filled your app ID in the widget URL: 'https://widget.intercom.io/widget/'
    (function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',w.intercomSettings);}else{var d=document;var i=function(){i.c(arguments);};i.q=[];i.c=function(args){i.q.push(args);};w.Intercom=i;var l=function(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/u9m06p0a';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);};if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})();
</script>
}

【问题讨论】:

  • JavaScript 不是 PHP,所有内容都应该写在单个脚本标签中,或者在顺序标签中,但不能嵌套脚本标签。
  • 我试图这样做,但它根本没有运行。对讲机提供了放置在

标签: javascript html wordpress if-statement tags


【解决方案1】:

你可以这样写:

<script>
<?php
    if(user.loggedin?) { ?>
      window.intercomSettings = {
        app_id: 'abc123',
        custom_launcher_selector:'#intercom',
        name: "<?=wp_get_current_user()->user_firstname?>", // Full name
        email: "<?=wp_get_current_user()->user_email?>", // Email address
        created_at: "<?=strtotime(wp_get_current_user()->created_at)?>" // Signup date as a Unix timestamp
      };
    // We pre-filled your app ID in the widget URL: 'https://widget.intercom.io/widget/u9m06p0a'
    (function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',w.intercomSettings);}else{var d=document;var i=function(){i.c(arguments);};i.q=[];i.c=function(args){i.q.push(args);};w.Intercom=i;var l=function(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/u9m06p0a';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);};if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})();
<?php } else { ?>
        window.intercomSettings = {
			app_id: 'abc123',
			custom_launcher_selector:'#intercom'
		};
    // We pre-filled your app ID in the widget URL: 'https://widget.intercom.io/widget/u9m06p0a'
    (function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',w.intercomSettings);}else{var d=document;var i=function(){i.c(arguments);};i.q=[];i.c=function(args){i.q.push(args);};w.Intercom=i;var l=function(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/u9m06p0a';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);};if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})();
<?php } ?>
</script>

【讨论】:

  • 感谢您的快速响应和支持。我已将此代码添加到 php 文件中,但出现导致角色返回的错误:由于文件 wp-content/footer.php 的第 26 行出现错误,您的 PHP 代码更改已回滚。请修复并再次尝试保存。语法错误,意外的 'if' (T_STRING)
猜你喜欢
  • 2014-11-11
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多