我正在构建一个网站,并且正在使用 PHP。我创建了一个名为 header.php
的文件(包含需要包含在我的所有页面上的 header )。
让我使用文件 header.php
的内容来更好地解释自己。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fashion</title>
<link rel="stylesheet" href="css/header.css"/>
<link rel="stylesheet" href="css/slider.css"/>
<link rel="stylesheet" href="css/bloc-page.css"/> <!--Load it when the user is on the page clothes.php-->
<link rel="stylesheet" href="css/footer.css"/>
<link rel="stylesheet" href="css/header-search.css"/>
<link rel="stylesheet" href="css/back-to-top.css"/>
<link rel="stylesheet" href="css/form-register.css"> <!--Load it when the user is on the page register.php-->
<link rel="stylesheet" href="css/form-login.css">
<link rel="stylesheet" type="text/css" href="css/style-carousel.css" /> <!--Load it when the user is on shoes.php-->
<link rel="stylesheet" type="text/css" href="css/owl.carousel.css" />
<link rel="stylesheet" type="text/css" href="css/owl.theme.default.min.css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/form-register.js"></script>
<script src="js/modernizr.js"></script> <!-- Modernizr -->
</head>
我面临的问题是,我想在某些页面上加载某些 CSS
文件。例如,我想在用户位于 clothes.php
bloc-page.css
我正在考虑使用正则表达式,但我想知道是否有更好的方法。
谢谢回复
最佳答案
在包含 header.php 之前,您可以创建一个包含所有 nesecary addon-css 的数组
<?php
$addonCss = Array("bloc-page.css");
include "header.php";
//... rest of page
?>
然后检查header.php中是否有这样的数组:
<?php
//....
if(isset($addonCss)) {
for($i=0;$i<count($addonCss); $i++) {
echo "<link rel=\"stylesheet\" href=\"css/".$addonCss[$i]."\"/>\n";
}
}
//...
?>
这只是多种可能性中的一种(这里是最简单的)。
关于javascript - 在 PHP 中的所有页面上包含 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37101479/