Php 在页眉的条件脚本中添加指向js和css文件的链接

Php 在页眉的条件脚本中添加指向js和css文件的链接,php,javascript,Php,Javascript,我想在我的页面标题中添加类似的内容,以便仅在不是iPhone/iPad的情况下加载js和css文件: if(!(navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) { //Link to js file //Link to css file } 第一行工作正常,我已经通过粘贴脚本对其进行了测试,但我不确定如何链接文件。。。特别是我希望js和css链接从WP-template

我想在我的页面标题中添加类似的内容,以便仅在不是iPhone/iPad的情况下加载js和css文件:

if(!(navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
{
    //Link to js file
    //Link to css file
}
第一行工作正常,我已经通过粘贴脚本对其进行了测试,但我不确定如何链接文件。。。特别是我希望js和css链接从WP-template标签开始

提前感谢您的帮助

诸如此类:

<?php if(!(navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) : ?>
    <link href="<?php echo get_stylesheet_directory_uri() ?>/main.css" type="text/css" rel="stylesheet" >
    <script src="<?php echo get_stylesheet_directory_uri() ?>/main.js" type="text/javascript"></script>
<?php endif ?>
诸如此类:

<?php if(!(navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) : ?>
    <link href="<?php echo get_stylesheet_directory_uri() ?>/main.css" type="text/css" rel="stylesheet" >
    <script src="<?php echo get_stylesheet_directory_uri() ?>/main.js" type="text/javascript"></script>
<?php endif ?>

最好使用PHP检查用户代理,因为添加正确的HTML标记以加载正确的css和js文件更容易

<?php if ( !preg_match( '/iPhone/', $_SERVER['HTTP_USER_AGENT'] ) && !preg_match( '/iPod/', $_SERVER['HTTP_USER_AGENT'] ) ): ?>
  <link href="<?php echo get_stylesheet_directory_uri() ?>/style.css">
  <script src="<?php echo get_stylesheet_directory_uri() ?>/script.js" type="text/javascript"></script>
<?php endif; ?>

仅使用javascript会增加不必要的复杂性,并且需要使用js加载器作为RequireJS

最好使用PHP检查用户代理,因为这样更容易添加正确的HTML标记来加载正确的css和js文件

<?php if ( !preg_match( '/iPhone/', $_SERVER['HTTP_USER_AGENT'] ) && !preg_match( '/iPod/', $_SERVER['HTTP_USER_AGENT'] ) ): ?>
  <link href="<?php echo get_stylesheet_directory_uri() ?>/style.css">
  <script src="<?php echo get_stylesheet_directory_uri() ?>/script.js" type="text/javascript"></script>
<?php endif; ?>

仅使用javascript会增加不必要的复杂性,并且需要使用js加载程序作为RequireJS

您可以直接在PHP中检查navigator服务器端,并使用WP api添加js和CSS文件。否则,您需要将目录URI作为JS变量传递到页面中,并在那里有条件地重复使用它。答案不是这样,而是if语句实际上在检查它是否不是iPhone或iPod。如果navigator.userAgent.match/iPhone/i | | | navigator.userAgent.match/iPod/i可能是您想要的试用版的可能副本,我想您可以直接在PHP中检查navigator服务器端,并使用WP api添加JS和CSS文件。否则,您需要将目录URI作为JS变量传递到页面中,并在那里有条件地重复使用它。答案不是这样,而是if语句实际上在检查它是否不是iPhone或iPod。如果navigator.userAgent.match/iPhone/i | | | navigator.userAgent.match/iPod/i可能是您想要的试用版的可能副本,我想可以了,谢谢Tomas。这就增加了代码,但是部件呢?e、 g.js链接需要是/yourscript.js谢谢Tomas。这就增加了代码,但是部件呢?e、 g.js链接必须是/yourscript.js谢谢seferov,但这会破坏页面!谢谢塞弗罗夫,但这会打破这一页!