All posts by acrane

About acrane

A web designer from Charlotte, NC, I enjoy working in the yard, kayaking, building stuff, making things, creating, thinking and of course WordPress.

Highlight a Navigation Item of the current page using jQuery

jQuery to find url and add an “active” class to that navigation item.

    $("[href]").each(function() {
        if (this.href == window.location.href) {
            $(this).addClass("active");
        }
    });

The CSS to style the active class:

.active {
    color: #ccc;
}

For Example:
If we had a Nav item like:


And we went to the About Page, we would see this:


Highlight a Parent Navigation item

What if you have an entire section that anytime you’re on that section the main nav is active?

// if I'm in "whatever-section" highlight my-main-nav-item. 
// example: http://mysite.com/whatever-section/a-page/
if(window.location.href.indexOf("whatever-section") > -1) {
       $('#main-nav .my-main-nav-item a').addClass("active");
}

Conditional for is page and page’s Children

In the functions file…

function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
	global $post;         // load details about this page
	if(is_page()&&($post->post_parent==$pid||is_page($pid))) 
               return true;   // we're at the page or at a sub page
	else 
               return false;  // we're elsewhere
};

In the theme…

if (is_tree(8)) { //the number being the page ID
   // do stuff
}