dongyan1808 2014-03-28 04:17
浏览 24
已采纳

如何将特定类应用于页面上的导航链接?

I have a navigation menu in my includes file, but I am having trouble echoing out the menu across my site. If I try and place the code as so, the page renders out mark up where the nav menu should be.

The code works somewhat if I do not add the <?php function before the menu.

I'm wondering how do I echo this code out within a function. I've tried changing the single quotes to double, but I think I am getting confused by the echo that is within the li in the menu.

A solution would be great.

Thanks

<?php
function nav() {
echo 
<nav class='container'>
<ul>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'current-journal')) echo 'class="current"';?> href="current-journal">CURRENT JOURNAL<span class="sub-nav">Our latest and greatest!</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submit')) echo 'class="current"';?> href="submit">SUBMIT<span class="sub-nav">Your writing</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submission-guidelines')) echo 'class="current"';?> href="submission-guidelines">SUBMISSION GUIDELINES<span class="sub-nav">Everything you need to know is here</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contributors')) echo 'class="current"';?> href="contributors">CONTRIBUTORS<span class="sub-nav">See who\'s in our magazine\'s latest issue</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'past-journals')) echo 'class="current"';?> href="past-journals">PAST JOURNALS<span class="sub-nav">Browse our issue archives</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'blog')) echo 'class="current"';?> href="blog">BLOG<span class="sub-nav">Just a blog</span></a></li>
</ul>
</nav>
}?>
  • 写回答

3条回答 默认 最新

  • doulingzou1712 2014-03-28 04:22
    关注

    The simplest is not to use the initial echo. Notice the ?> after the function name and <?php added again at the bottom for the ending function curly brace.

    <?php
    function nav() {
    ?>
    <nav class='container'>
    <ul>
    <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'current-journal')) echo 'class="current"';?> href="current-journal">CURRENT JOURNAL<span class="sub-nav">Our latest and greatest!</span></a></li>
    <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submit')) echo 'class="current"';?> href="submit">SUBMIT<span class="sub-nav">Your writing</span></a></li>
    <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submission-guidelines')) echo 'class="current"';?> href="submission-guidelines">SUBMISSION GUIDELINES<span class="sub-nav">Everything you need to know is here</span></a></li>
    <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contributors')) echo 'class="current"';?> href="contributors">CONTRIBUTORS<span class="sub-nav">See who\'s in our magazine\'s latest issue</span></a></li>
    <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'past-journals')) echo 'class="current"';?> href="past-journals">PAST JOURNALS<span class="sub-nav">Browse our issue archives</span></a></li>
    <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'blog')) echo 'class="current"';?> href="blog">BLOG<span class="sub-nav">Just a blog</span></a></li>
    </ul>
    </nav>
    <?php
    }?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建