Parent Highlight when Subpage is showing
-
Hi Everyone
My site is: http://www.jordimas.eu
I’d like to keep highlights on the parent page menu while a subpage is showing (and also use highlight on the submenu link). Is there any way to do this?Thanks a lot!
-
I was able to keep highlights on the parent pages’ menu while viewing subpages by hardcoding pages’ IDs into the menu:
<a <?php if (is_page('2')) { echo "class=\"current\""; } elseif (is_page('3')) { echo "class=\"current\""; } elseif (is_page('4')) { echo "class=\"current\""; } // and so on ?>This is not the best solution, I guess, but as a non-pro I couldn’t find another. If there is a better way to make this work (especially with lots of subpages), it would be realy great to know.
Guess I found a better solution, having re-read the Conditional Tags article from the Codex. 🙂 I’m using it in the a tag, since putting it in the li tag doesn’t work for me:
<a if ( is_page('') && $post->post_parent ) { echo "class=\"current\""; } elseif (is_page('5')) { echo "class=\"current\""; } else { // display nothing } ?> href="some_link_fetching_tag_here">If li tags works for your template, you can put this code in it:
<li if ( is_page('') && $post->post_parent ) { echo "class=\"current\""; } elseif (is_page('5')) { echo "class=\"current\""; } else { // display nothing } ?>>This piece of script is obviously looking to see whether the page displaying is a subpage of a page which id you’ve specified in the line 3, and if it is it adds a special class to the link. If it is that page itself, it also adds a special class to the link. If it’s some other page, it does nothing.
The downside is that you can’t use it with a dynamic menu which is built automatically. I’m building the menu by hand, so it works okay for me. Probably there are other ways to achieve what you need.
Hi, I finally used this and it rocks:
<ul class=”menu”>
- <span>Blog</span>
<?php echo preg_replace(‘@\<li([^>^]*)>\<a([^>^]*)>(.*?)\<\/a>@i’,'<li$1><a$2><span>$3</span>’ ,wp_list_pages(‘echo=0&depth=1&title_li=’));?>
Thank you, sergislevas, your code was exactly what I wanted. That’s the best solution I’ve found for a dynamic menu.
The topic ‘Parent Highlight when Subpage is showing’ is closed to new replies.