Apply a specific class/id to current page on menu (PHP) -
i have menu this:
<div id="blahblah" style="blahblah"> <a href="http://domain.com/folder/biography"><img style="blahblah" src="blahblahblahblah"></a> <a href="http://domain.com/folder/contacts"><img style="blahblah" src="blahblahblahblah"></a> <a href="http://domain.com/folder/gallery"><img style="blahblah" src="blahblahblahblah"></a> <a href="http://domain.com/folder/dontknow"><img style="blahblah" src="blahblahblahblah"></a> </div> i'd have automatically adds class="current" page i'm in. links (as can see in code above) domain.com/folder/biography or domain.com/folder/contacts, without .php/.html, etc.
i tried with:
<div id="blahblah" style="blahblah"> <a <?php if (strpos($_server['php_self'], 'biography')) echo 'class="current"';?> href="http://domain.com/folder/biography"><img style="blahblah" src="blahblahblahblah"></a> <a <?php if (strpos($_server['php_self'], 'contacts')) echo 'class="current"';?> href="http://domain.com/folder/contacts"><img style="blahblah" src="blahblahblahblah"></a> ... ... </div> but doesn't work... solution strops seems viable, i'm doing wrong.. :p
you should:
- check result of
strpos()!== false. - use
$_server['request_uri']rather$_server['php_self']. - wrap code inside function.
something this:
<?php function get_current($name) { if (strpos($_server['request_uri'], $name) !== false) echo 'class="current"'; } ?> <div id="blahblah" style="blahblah"> <a <?php get_current('biography') ?> href="http://domain.com/folder/biography"><img style="blahblah" src="blahblahblahblah"></a> <a <?php get_current('contacts') ?> href="http://domain.com/folder/contacts"><img style="blahblah" src="blahblahblahblah"></a> ... ... </div>
Comments
Post a Comment