[英]Different url display different div - header.php
我可能會問一個愚蠢或愚蠢的問題。 但是,我很好奇,因為我是php的初學者。 我想問一下這種方式是否可能發生。
問題來了:我想要做一些事情,例如WordPress。 但是,我不會在此站點中使用wordpress。 就像wordpress在其路徑中具有header.php。 然后,我們可以使用wordpress php代碼來顯示或隱藏我們想要顯示或隱藏的內容,如下所示。
以下是我從Wordpress復制的php代碼作為示例:
<?php if (is_front_page()) { ?><?php } elseif (is_home() || is_single() || is_page() || is_archive() || is_404() || is_search()) { ?>
<?php
/* We add some JavaScript to pages with the comment form
* to support sites with threaded comments (when in use).
*/
if ( is_singular() && get_option( 'thread_comments' ) )
wp_enqueue_script( 'comment-reply' );
/* Always have wp_head() just before the closing </head>
* tag of NCC, or you will break many plugins, which
* generally use this hook to add elements to <head> such
* as styles, scripts, and meta tags.
*/
wp_head();
?>
<?php } ?>
因為,我將創建一個使用簡單的php編碼實現的簡單網站。 我將創建一些頁面-> page1.php
, page2.php
, page3.php
和header.php
。 header.php
用於管理網頁的頭部(如wordpress的操作方式)。 是否有可能通過簡單的php代碼完成它?
示例:當我訪問page2.php
, header.php
將僅顯示我想要顯示的內容。
是否可以將is_single()
更改為url
? 有什么建議嗎? 還是任何其他更好的解決方案可以使此操作更容易?
更新: header.php
<?php
function includeFile($file){
if($file == "index.php"){
Page Title for index.php
}elseif($file == "page.php"){
Page Title for page.php
}elseif($file == "page1.php"){
Page Title for page1.php
}
}
?>
在我的index.php
<?php session_start(); /* Starts the session */
if(!isset($_SESSION['UserData']['Username'])){
header("location:login.php");
exit;
}
?>
<?php include('functions.php'); ?>
<!doctype html>
<html>
<head>
<title>
<?php
require_once('header.php');
includeFile(basename(__FILE__));
?>
</title>
<link href="style.css" rel="stylesheet" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<link href='http://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' />
<link rel="shortcut icon" href="http://www.example.com/img/favicon.ico" type="image/x-icon">
</head>
<body>
Content...
</body>
</html>
更新:
是的,您當然可以使用PHP魔術常量 __FILE__
和basename()做到這一點。
header.php
function includeFile($file){
if($file == "page1.php"){
// include stuff for page1.php
}elseif($file == "page2.php"){
// include stuff for page2.php
}elseif($file == "page3.php"){
// include stuff for page3.php
}
}
並且在每個頁面中都包含頭文件並調用函數includeFile()
,如下所示:
page1.php
<?php
require_once('header.php');
includeFile(basename(__FILE__));
?>
page2.php
<?php
require_once('header.php');
includeFile(basename(__FILE__));
?>
page3.php
<?php
require_once('header.php');
includeFile(basename(__FILE__));
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.