I need to assign an "active" class to my main-level navigation rendered by wordpress.
Here's my HTML:
<li><a href="/">Home</a></li>
<?php wp_list_pages('title_li=&exclude=21'); ?>
I need to assign an "active" class to my main-level navigation rendered by wordpress.
Here's my HTML:
<li><a href="/">Home</a></li>
<?php wp_list_pages('title_li=&exclude=21'); ?>
See WP Codex for adding classes/ids to your wp template:
<li<?php
if (is_home()) {
echo " class=\"active\"";
}
?>><a href="/">Home</a></li>
WordPress automatically adds relevant classes to list items that are made using the wp_list_pages() function. For example, it will add a class of current_page_item to the page which you are currently viewing - allowing you to style this particular list item differently.
This only works for pages and afaik does NOT work for posts. However, there is a slightly hack-ish way around it. If you have a list of posts from your loop, each post will have a class with post-## -- where ## is the ID of the post (as long as you use the get_post_class() function, or similar)
You can also get the ID of the current post being displayed by:
Outside of the list of posts you are outputting, preferably in your header.php, have:
$this_posts_id = $post->ID;
Then inside of the loop of the list of posts you are making:
if($this_posts_id == $post->ID;){echo "current";}
or something similar!
Hope that helps