Tuesday, February 5, 2008

Taw#6: Dynamic Menus in Wordpress

Okay, its not a photographic technique but it is something I learned this week that was used for my photographic website, 10framesasecond.com - dynamic menus. Initially I was going to build a website from scratch, and I started out doing that but then I found out how easy wordpress is to use, and the blog format suited routine updates to the site without any further coding. However, one thing I didn't know how to hack into wordpress was how to highlight the current location the user was at in the navigation menu. In HTML code I just would have the change the appropriate menu item's class when the page loaded. But in wordpress, you don't really work in HTML directly and my menu was in the static header and doesn't change with each new page load. I notice one of my templates using an is_home function and I dug a little deeper and found the is_page function which would let me do exactly what I wanted - here's my code:

if(is_page('products')){echo 'class="here"';}

just place this little bit of php in the opening 'a' tag and if the page your user is on is called 'products' it will insert the 'class="here" into the HTML which will cause the browser to used the css info to highlight the item.
pretty simple once ya get the right function. A more comprehensive explanation is here.
Post a Comment