Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you're looking for a non-javascript / php approach...</p> <p>First you need to determine which nav-link should be set as active and then add the <em>selected</em> class. The code would look something like this</p> <p><strong>HTML within php file</strong></p> <p>Call a php function inline within the hyperlink <code>&lt;a&gt;</code> markup passing in the links destination request uri</p> <pre><code>&lt;ul&gt; &lt;li&gt;&lt;a href="index.php" &lt;?=echoSelectedClassIfRequestMatches("index")?&gt;&gt;Home&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="about.php" &lt;?=echoSelectedClassIfRequestMatches("about")?&gt;&gt;About&lt;/a&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="services.php" &lt;?=echoSelectedClassIfRequestMatches("services")?&gt;&gt;Services&lt;/a&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="features.php" &lt;?=echoSelectedClassIfRequestMatches("features")?&gt;&gt;Features&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Support&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="support1.php" &lt;?=echoSelectedClassIfRequestMatches("support1")?&gt;&gt;Support 1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="support2.php" &lt;?=echoSelectedClassIfRequestMatches("support2")?&gt;&gt;Support 2&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;/ul&gt; </code></pre> <p><strong>PHP function</strong></p> <p>The php function simply needs to compare the passed in request uri and if it matches the current page being rendered output the <em>selected</em> class</p> <pre><code>&lt;?php function echoSelectedClassIfRequestMatches($requestUri) { $current_file_name = basename($_SERVER['REQUEST_URI'], ".php"); if ($current_file_name == $requestUri) echo 'class="selected"'; } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload