我目前正在开发一个网站,目的是为了学习更多,但我只是想不出这个,我不知道该搜索到什么,我还没找到。
所以基本上我有一个导航栏,一个内容框和一个页脚。我想将网站划分为三个文件。这样,我只需要编辑一个文件来编辑所有页面上导航栏中的所有链接。
我可以通过以下方式完成此操作:
<?php include('navigation.php'); ?>
我希望它在哪里。 但是我的问题出现了:在我拥有的每个页面上,我的导航栏应该更改其活动页面/标签并突出显示它。
我的导航栏看起来像这样:
Home | News | About | Contact
当我点击News
并登陆新闻页面时,它应该会在导航栏中突出显示(通过CSS)。但是,当我将导航栏放在一个文件中时,如何实现这一目标?然后它会在所有页面上突出显示它。这是我目前遇到的问题,如果在PHP中甚至可以做到这一点我也不知道?
任何帮助表示赞赏!感谢
答案 0 :(得分:3)
最简单的方法:设置一个全局变量来说&#34;其中&#34;你是,并有导航菜单检查:
e.g。
的index.php:
<?php
$PAGE = 'home';
include('navigation.php');
navigation.php:
<?php
...
if (isset($PAGE) && ($PAGE == 'home')) {
.... output "home" link with you-are-here highlight
} else {
... output regular home link.
}
答案 1 :(得分:3)
您也许可以检查当前网址是什么,并相应地在菜单项上添加活动课程。
<li class='<?php echo ($url == "about.php") ? "active" : ""?>' >About</li>
然后当您生成菜单链接时,如下所示:
{{1}}
这些方面的东西。
答案 2 :(得分:0)
从p
网址处理获取网页。检查是否允许,否则回家。
然后在菜单中检查页面当前是否处于活动状态,如果是,添加课程active
。
<?php
// Get the page from the url, example: index.php?p=contact
$page = $_GET['p'];
// Whitelist pages for safe including
$whitelist = array('home', 'news', 'about', 'contact');
// Page not found in whitelist
if (!in_array($page, $whitelist)):
$page = 'home';
endif;
include('header.php');
include('navigation.php');
include($page . '.php'); // Include page according to url
include('footer.php');
?>
<ul>
<li>
<a href="index.php?p=home" class="<?php if ($page === 'home'): ?>active<?php endif; ?>">
Home
</a>
</li>
<li>
<a href="index.php?p=news" class="<?php if ($page === 'news'): ?>active<?php endif; ?>">
News
</a>
</li>
<li>
<a href="index.php?p=about" class="<?php if ($page === 'about'): ?>active<?php endif; ?>">
About
</a>
</li>
<li>
<a href="index.php?p=contact" class="<?php if ($page === 'contact'): ?>active<?php endif; ?>">
Contact
</a>
</li>
</ul>