使用 php 将<链接>添加到<head>


Adding <link> to <head> Using php

我有一个页面,其中包含样式表的链接标签和javascript文件的脚本,我想将这些脚本附加到页面的head部分,但使用php而不使用jQuery。

感言.php

$ex_style = '<link href="'.$_path_.' /css/style.css" rel="stylesheet" type="text/css" />';
$ex_script = '<script src="'.$_path_.' /js/scripts.js" type="text/javascript" />';
// My PHP Code 
...

如何使用上面的$ex_style$ex_script使用 php 并附加到页面头部?

让我们这样说吧。

我有一个安装在 www.domain-a.com 上的推荐脚本,我想使用 php 函数在侧边栏中显示推荐 www.domain-b.com,例如

include('http://www.domain-a.com/script/testimonial.php');

如果testimonial.php只包含php代码和javascript和css,它没有<head><body>元素。

示例 :在 Wordpress 中wp_enqueue_script函数中有一个参数,我们可以在其中设置它以在页脚中添加脚本。

您不能在包含函数中访问绝对 URL。

请参阅以下示例:-

相对路径

索引.html

/

图形/图像.png

/help/articles/how-do-i-set-up-a-webpage.html

绝对路径

http://www.mysite1.com

http://www.mysite2.com/graphics/image.png

http://www.mysite3.com/help/articles/how-do-i-set-up-a-webpage.html

您会注意到两种不同类型的链接之间的第一个区别是,绝对路径始终包含网站的域名,包括 http://www.,而相对链接仅指向文件或文件路径。当用户单击相对链接时,浏览器会将他们带到当前网站上的该位置。因此,您只能在链接到网站中的页面或文件时使用相对链接,如果您要链接到其他网站上的某个位置,则必须使用绝对链接。

另请参阅此链接。

希望它能帮助你:)

使用 include 方法,就像那 2 个示例文件一样

vars.php
 <?php 
  $color = 'green';
  $fruit = 'apple';
 ?>
test.php
<?php echo "A $color $fruit"; // will return A
  include 'vars.php';    
  echo "A $color $fruit";// will return  A green apple 
?>

或阅读此页面:http://php.net/manual/en/function.include.php

非常简单的解决方案:

<head>
<?php
 // use require_once not include
 require_once 'external.php';
 echo $ex_style . $ex_script
?>
</head>