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>
}?>
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
}?>
Replace with this:
<?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>
You dont need echo for the showing the html just close the php tag before the html
Hope this will help!!
<?php
function nav() {
?>
<nav class='container'>
<ul>
<?php if(strpos($_SERVER['PHP_SELF'],'current-journal' )) {echo "<li><a class='current' href='current-journal'>CURRENT JOURNAL<span class='sub-nav'>Our latest and greatest!</span></a></li>"; }?>
<?php if(strpos($_SERVER['PHP_SELF'],'submit' )) {echo "<li> <a class='current' href='submit'>SUBMIT<span class='sub-nav'>Your writing</span></a></li>"}?>
<?php if(strpos($_SERVER['PHP_SELF'],'submission-guidelines' )) {echo "<li> <a class='current' href="submission-guidelines'>SUBMISSION GUIDELINES<span class="sub-nav">Everything you need to know is here</span></a></li>";}?>
<?php if(strpos($_SERVER['PHP_SELF'],'contributors' )) {echo "<li> <a class='current' href='contributors'>CONTRIBUTORS<span class='sub-nav'>See who\'s in our magazine\'s latest issue</span></a></li>";}?>
<?php if(strpos($_SERVER['PHP_SELF'],'past-journals' )) {echo "<li> <a class='current' href='past-journals'>PAST JOURNALS<span class='sub-nav'>Browse our issue archives</span></a></li> ";}?>
<?php if(strpos($_SERVER['PHP_SELF'],'blog' )) {echo "<li> <a class='current' href='blog'>BLOG<span class='sub-nav'>Just a blog</span></a></li>";}?>
</ul>
</nav>
<?php
}
?>
This should do the work.