HTML CSS a:current

Soldato
Joined
3 Feb 2003
Posts
2,856
Location
Shropshire
Evening, help needed desperately as ive exhausted my skill set

I am creating a basic menu using <ul> <li> but i want to use a.current as a class to highlight the selected page i'm on.

Now - ive got it up and running for a absolute url but id like to use only a part url so anything within a directory and sub directories are highlighted too.

Anyone know if this is possible? I cant find any information as i am not quite even sure what im looking for. Some sort of wildcard for the url??

TIA
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
It will probably be something like:
.current ul li{ **highlighting code** }
So that you are selecting all of the sub items contained within the .current item. Depends on how your page is structured though.
Is it a live site that we can look at?
 
Associate
Joined
21 May 2013
Posts
1,991
I can't work out what you're trying to do from your description. Maybe draw a picture to explain it?
 
Soldato
OP
Joined
3 Feb 2003
Posts
2,856
Location
Shropshire
Yeah PHP

Code:
<{php}>  
$pageurl = getenv("REQUEST_URI"); 
 $GLOBALS['Tpl']->assign( 'pageurl', $pageurl); 
 <{/php}>

Code:
    <div id="nav" class="<{$pageurl}>">
      <ul>
        <li class="<{if $pageurl == "/"}>current<{/if}>"><a href="<{AppUrl }>">Home</a></li>

The problem lies if i select a page that is not an exact url. The link in the nav bar is not highlighted.

ie /news/index.php is highlighted but /news/page1.php isnt.
Id like everything in directory /news/ to be highlight in the navigation menu.


Make sense?
 
Soldato
OP
Joined
3 Feb 2003
Posts
2,856
Location
Shropshire
It will probably be something like:
.current ul li{ **highlighting code** }
So that you are selecting all of the sub items contained within the .current item. Depends on how your page is structured though.
Is it a live site that we can look at?

Sorry not a live site yet :( its not actually working yet
 
Associate
Joined
29 Jul 2014
Posts
10
Location
Peterborough
You'll want to be doing something like this:

<div id="nav" class="<{$pageurl}>">
<ul>
<li class="<{if $pageurl == "/"}>current<{/if}>"><a href="<{AppUrl }>">Home</a></li>
<li class="<{if substr($pageurl, 0, 5) == "/news"}>current<{/if}>"><a href="<{AppUrl }>">News</a></li>

This just compares the first part of your URL (that containts '/news') and checks that it matches the first section.

So with my example, all of these URL's would apply the 'current' class to your li tag:

/news
/news/woohoo
/news/news-title
/news/woop/herp/derp

So as long as your URL schema is structured so that anything related to news is under /news, it should work. :)
 
Associate
Joined
21 May 2013
Posts
1,991
You could use something like:
$uri = explode('/', $_SERVER['REQUEST_URI']);
then $uri becomes an array of all of the URI components.
It's an easier to use alternative to checking substr all the time.
 
Soldato
Joined
19 Jul 2009
Posts
7,243
aye, that's the way.

You could also create a function that writes the the list items / anchors / class

Then call the function with call_function($uri, $linkText); Put the function in an include and neatness abounds.
 
Back
Top Bottom