我希望在我的所有网站页面中都有动态header.php,并且有很多CSS文件可供加载。有没有办法为每个页面加载不同的CSS文件?例如:
<?php if($title == "title") { ?>
<link href="mycss.css" >
<script src="test.js"></script>
<?php } ?>
答案 0 :(得分:1)
首先检查此网址路径
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
?>
或
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$url1 = "http://example.com/home";
$url2 = "http://example.com/about";
if (strpos($url1,'home') !== false) { ?>
<link href="mycss.css" >
<?php
} else if(strpos($url2,'about') !== false){ ?>
<link href="mycss2.css" >
<?php }else { ?>
// your defult css file
<?php } ?>
我希望你理解
答案 1 :(得分:1)
以一种简单的方式,您可以使用下面给出的这种方法......
<?PHP
$page_name= $_SERVER['PHP_SELF'];
if($page_name=='about.php'){
echo '<link href="about_css.css" type="text/css">
<script src="about_test.js"></script>';
}
if($page_name=='contact.php'){
echo '<link href="contact_css.css" type="text/css">
<script src="contact_test.js"></script>';
}
?>
答案 2 :(得分:1)
将此index.php文件放在css文件夹中
<?
$title = $_GET['title'];
header('Content-Type: text/css');
switch ( $title ) {
case 'title_1':
include('style_1.css');
case 'title_2':
include('style_2.css');
default:
include('default.css');
}
exit();
?>
然后,只要你想打电话给你的css:
<?
echo '<link href="path/to/css/folder/?title='.$variable.'" rel="stylesheet">'
?>
css文件夹中的index.php将是需要包含css的控制器
答案 3 :(得分:0)
为简单起见,您可以为每个页面分配页面代码。使用该名称创建脚本。所以你可以像这样导入它:
在页面标题中:
$pageCode = "INDEX";
在页脚中:
<?php
$scriptFileName = "your/path/to/file/".$pageCode.".js";
if(file_exists($scriptFileName)){
echo "<script src='$scriptFileName'></script>";
}
如果需要,对CSS应用相同的内容。但是在标题中!
答案 4 :(得分:0)
您可以创建一个函数(或类)来将标题和Feed参数作为设置进行回显。这样的事情可能有用:
<强> function.get_header.php 强>
// This function will render the header
function get_header($settings = false)
{
ob_start();
include("header.php");
$data = ob_get_contents();
ob_end_clean();
return $data;
}
<强> function.get_page_css.php 强>
// This function will act like a pseudo database return
// and will return a series of css links based on input
function get_page_css($var = false)
{
$css['title'][] = "/css/style1.css";
$css['other'][] = "/css/style2.css";
$css['title'][] = "/css/style3.css";
if(!empty($css[$var]))
return $css[$var];
}
<强>的header.php 强>
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title><?php echo (!empty($settings['title']))? $settings['title'] : "Untitled Page"; ?></title>
<head>
<?php if(!empty($settings['css']) && is_array($settings['css'])) {
foreach($settings['css'] as $link) {
?>
<link type="text/css" rel="stylesheet" href="<?php echo $link; ?>" />
<?php }
}
?>
</head>
<强>的index.php 强>
<?php
// Include the header function
include("function.get_header.php");
// Include the css return function
include("function.get_page_css.php");
// Write the header to browser using the get_page_css() function
echo get_header(array("title"=>"This Great Page!","css"=>get_page_css('title')));
?>
<body>...etc.
答案 5 :(得分:0)
这对我来说很好,我将其添加到了我所有页面上包含的_header.php文件中:
<?php
$page_name = $_SERVER['PHP_SELF'];
if($page_name =='/recepiepg/admin/index.php'){
echo '<link rel="stylesheet" href="../css/admin.css" type="text/css"/>';
}
?>