不同的url显示不同的div-header.php
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 magic constant __FILE__
and 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__));
?>
我可能问了一个愚蠢或愚蠢的问题。但是,我很好奇,因为我是 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 magic constant __FILE__
and 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__));
?>