Php 如何将当前类添加到导航列表项和链接中?

Php 如何将当前类添加到导航列表项和链接中?,php,navigation,Php,Navigation,我尝试了几种不同的方法在我的导航上添加一个current类 我在以下网站上读到一篇关于这样做的文章: 到目前为止,我还没有把这些方法整合到我的网站上 我觉得我错过了什么 我使用名为nav.php的includes文件将导航拉入站点: <div id="nav"> <div id="nav-container"> <ul id="nav-content" class="top menu"> <li&l

我尝试了几种不同的方法在我的导航上添加一个current类

我在以下网站上读到一篇关于这样做的文章:

到目前为止,我还没有把这些方法整合到我的网站上

我觉得我错过了什么

我使用名为nav.php的includes文件将导航拉入站点:

    <div id="nav">
    <div id="nav-container">
        <ul id="nav-content" class="top menu">
            <li<?php if ($thisPage=="home") 
            echo " id=\"currentpage\""; ?>>
            <a href="/index2013.php">Home</a></li>

            <li<?php if ($thisPage=="portfolio") 
            echo " id=\"currentpage\""; ?>>
            <a href="/design/designsystems/unitedway/unitedway.php">Portfolio</a></li>

            <li<?php if ($thisPage=="services") 
            echo " id=\"currentpage\""; ?>>
            <a href="/services/services.php">Services</a></li>

            <li<?php if ($thisPage=="resources") 
            echo " id=\"currentpage\""; ?>>
            <a href="/resources/resources.php">Resources</a></li>

            <li<?php if ($thisPage=="blog") 
            echo " id=\"currentpage\""; ?>>
            <a href="http://kristinemorical.com/wordpress/kristine-blog/">Blog</a></li>

            <li<?php if ($thisPage=="contact") 
            echo " id=\"currentpage\""; ?>>
            <a href="/contactinfo/contact.php">Contact</a></li>
        </ul>
    </div>
    </div>

    > > >
这是我在索引页正文中的内容:

<?php $thisPage="home"; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Kristine Morical : Home</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="author" content="Kristine Morical" />
<?php include('http://www.kristinemorical.com/includes/head.php'); ?>
</head>

    <body id="body">

        <?php include('http://www.kristinemorical.com/includes/header.php'); ?>

        <?php include('http://www.kristinemorical.com/includes/nav.php'); ?>

        <div id="outer" class="clearfix">

        <div id="sidebar-wrapper">
        <?php //include('http://www.kristinemorical.com/includes/sidebar.php'); ?>
        </div>

        <div id="content" role="main">      

        </div> <!-- end #main -->

        </div> <!-- end #outer -->

        <?php include('http://www.kristinemorical.com/includes/footer.php'); ?>

    </body>

</html>

克里斯汀·莫里卡尔:家
我不确定我做错了什么

首先,您可以将条件用于更干净的代码,其次,使用css
来简化样式(如果添加其他导航元素,这将允许多个):

试试这个:

<div id="nav">
    <div id="nav-container">
        <ul id="nav-content" class="top menu">
            <?php if ($thisPage=="home")  echo "<li id=\"currentpage\">"; else echo "<li>";?>
            <a href="/index2013.php">Home</a></li>

            <?php if ($thisPage=="portfolio")  echo "<li id=\"currentpage\">"; else echo "<li>";?>
            <a href="/design/designsystems/unitedway/unitedway.php">Portfolio</a></li>

            <?php if ($thisPage=="services")  echo "<li id=\"currentpage\">"; else echo "<li>";?>
           <a href="/services/services.php">Services</a></li>

            <?php if ($thisPage=="resources")  echo "<li id=\"currentpage\">"; else echo "<li>";?>
            <a href="/resources/resources.php">Resources</a></li>

            <?php if ($thisPage=="blog")  echo "<li id=\"currentpage\">"; else echo "<li>";?>
            <a href="http://kristinemorical.com/wordpress/kristine-blog/">Blog</a></li>

            <?php if ($thisPage=="contact")  echo "<li id=\"currentpage\">"; else echo "<li>";?>
           <a href="/contactinfo/contact.php">Contact</a></li>
        </ul>
    </div>
</div>

如果您使用的是ID标记,那么这将起作用,但是如果您确实想使用类,那么您应该使用类标记。这取决于您在网页的head标签或css文件中声明类的方式。如果您用#号声明类,那么上面的代码可以工作,但这不是最佳实践。您可能希望使用句号来声明类,然后在网页中使用类标记

<div id="nav">
    <div id="nav-container">
        <ul id="nav-content" class="top menu">
            <?php if ($thisPage=="home")  echo "<li id=\"currentpage\">"; else echo "<li>";?>
            <a href="/index2013.php">Home</a></li>

            <?php if ($thisPage=="portfolio")  echo "<li id=\"currentpage\">"; else echo "<li>";?>
            <a href="/design/designsystems/unitedway/unitedway.php">Portfolio</a></li>

            <?php if ($thisPage=="services")  echo "<li id=\"currentpage\">"; else echo "<li>";?>
           <a href="/services/services.php">Services</a></li>

            <?php if ($thisPage=="resources")  echo "<li id=\"currentpage\">"; else echo "<li>";?>
            <a href="/resources/resources.php">Resources</a></li>

            <?php if ($thisPage=="blog")  echo "<li id=\"currentpage\">"; else echo "<li>";?>
            <a href="http://kristinemorical.com/wordpress/kristine-blog/">Blog</a></li>

            <?php if ($thisPage=="contact")  echo "<li id=\"currentpage\">"; else echo "<li>";?>
           <a href="/contactinfo/contact.php">Contact</a></li>
        </ul>
    </div>
</div>