php ‘included’ menu styling

Some times, when we have a pretty big amount of pages on a site but don’t need or want to use a CMS so we take advantage of PHP and split the page layout in more includes like:header.php, content.php, footer.php – this way we do not need to write the same content (or copy paste it :D ) in every page.

If our design is user centered ( i mean it tries to have some usability ) we’ll need to find a way to make the menu clear – pages that are active needs to be “highlighted” somehow. A good example is WordPress :

menu highlighted

“Posts” page is the current page. We can do this the hard way – copy menu content in every page and change the styling for current link, or use some php code that do this for us and move the menu in “header.php” include file.This is a small code we can use for our needs:

<a class="<?php echo (( $url=='href="index.php')); ?>">Home</a>
<a class="<?php echo (( $url=='href="product.php')); ?>">Product</a>
<a class="<?php echo (( $url=='href="directory.php')); ?>">Directory</a>

The above code will work for relative links, for absolute links you will have to detect the full url of current page:

<?php 
    $domain = $_SERVER['HTTP_HOST'];
    $url = "http://" . $domain . $_SERVER['REQUEST_URI'];
?>
 
<div class="actual_menu">
<a href="http://www.cssgallery.info/index.php"	
class="<?php echo (( $url=='http://www.cssgallery.info/index.php') ? "menu_on" : "menu")?>;"
>Home</a></div>
 

No related posts.