Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In your case, I would solve it like this:</p> <pre><code>&lt;? function get_current( $nav_page ) { $uri = $_SERVER['REQUEST_URI']; return ( $uri == $nav_page ) ? ' id="current" ' : ''; } ?&gt; </code></pre> <p>And small changes in HTML:</p> <pre><code>&lt;ul&gt; &lt;li &lt;?= get_current('/') ?&gt; &gt;&lt;a&gt;HOME&lt;/a&gt;&lt;/li&gt; &lt;li &lt;?= get_current('/page1.php') ?&gt; &gt;&lt;a&gt;PAGE 1&lt;/a&gt;&lt;/li&gt; &lt;li &lt;?= get_current('/page2.php') ?&gt; &gt;&lt;a&gt;PAGE 2&lt;/a&gt;&lt;/li&gt; &lt;li &lt;?= get_current('/page3.php') ?&gt; &gt;&lt;a&gt;PAGE 3&lt;/a&gt;&lt;/li&gt; &lt;li &lt;?= get_current('/page4.php') ?&gt; &gt;&lt;a&gt;PAGE 4&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>Please note that I changed the parameters to absolute URLs because <code>$_SERVER['REQUEST_URI']</code> contains absolute path.</p> <h2>EDIT:</h2> <p>Since you used <code>func_get_args()</code>, you might be going to accept arbitrary number of 'page-name.php' as parameter for <code>get_current()</code>. In that case, the <code>get_current</code> function becomes:</p> <pre><code>function get_current() { $uri = $_SERVER['REQUEST_URI']; if ( in_array( $uri, func_get_args() ) ) return ' id="current" '; return ''; } </code></pre> <p>and its accompanying HTML:</p> <pre><code>&lt;li &lt;?= get_current('/', '/index.php')?&gt; &gt; ... &lt;/li&gt; </code></pre> <h2>EDIT2:</h2> <p>Your <code>$_SERVER['REQUEST_URI']</code> shows that files are in the <code>/new/</code> folder. Changed the logic to compare end-parts. </p> <pre><code>&lt;? function get_current( $nav_page ) { $uri = $_SERVER['REQUEST_URI']; return is_rear_match( $uri, $nav_page ) ? ' id="current" ' : ''; } function is_rear_math( $haystack, $needle ) { $rear = substr($haystack, -strlen($needle)); return $rear !== false &amp;&amp; $rear === $needle; } ?&gt; </code></pre> <p>HTML:</p> <pre><code>&lt;ul&gt; &lt;li &lt;?= get_current('/') ?&gt; &gt;&lt;a&gt;HOME&lt;/a&gt;&lt;/li&gt; &lt;li &lt;?= get_current('/page1.php') ?&gt; &gt;&lt;a&gt;PAGE 1&lt;/a&gt;&lt;/li&gt; &lt;li &lt;?= get_current('/page2.php') ?&gt; &gt;&lt;a&gt;PAGE 2&lt;/a&gt;&lt;/li&gt; &lt;li &lt;?= get_current('/page3.php') ?&gt; &gt;&lt;a&gt;PAGE 3&lt;/a&gt;&lt;/li&gt; &lt;li &lt;?= get_current('/page4.php') ?&gt; &gt;&lt;a&gt;PAGE 4&lt;/a&gt;&lt;/li&gt; &lt;/ul&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