php - echo class "active" - if class="" is already exists or not. -
the following php function add class="active" current page open.
<?php #add class .active current page $directoryurl = $_server['request_uri']; $path = parse_url($directoryurl, php_url_path); $components = explode('/', $path); $currentpage = preg_replace("/\\.[^.\\s]{3,4}$/", "", end($components)); if ($currentpage == "") { $currentpage = "index"; } function href($url) { global $currentpage; $path = explode('/', $url); $page = preg_replace("/\\.[^.\\s]{3,4}$/", "", end($path)); echo 'href="' . $url . '"'; if ($page == $currentpage) { echo 'active'; } } ?>
here menu items:
<li><a class="icon-glass"<?php href('index.php');?>>home</a></li> <li><a <?php href('about.php');?>>about</a></li>
the problem is, function works menu items has no class="" declared, i.e:
<li><a <?php href('about.php');?>>about</a></li>
and not work one:
<li><a class="icon-glass"<?php href('index.php');?>>home</a></li>
i believe part of script:
if ($page == $currentpage) { echo 'class="active"'; }
the question: how keep now, adding class=""
when not added, add .active
bit inside existed class (if found)?
i tried use echo 'active';
didn't work.
try format existing php code
<li><a class="icon-glass <?php href('index.php');?>">home</a></li> <li><a class="<?php href('about.php');?>">about</a></li>
means have echo class active inside class property double quotes "".
Comments
Post a Comment