Determine Current Page in WordPress

Lately, I’ve been doing a lot of work with WordPress themes. On one particular site, the navigation menu is built dynamically, combining pages and categories in the navigation scheme. I needed to find a way to determine which page or category was currently being viewed so I could apply a special class to the item in the navigation menu, indicating that the link is active.

After a bit of research, I found a couple of useful WordPress functions that helped me determine which page the visitor is currently viewing.

Home Page

First, let’s look at a function that indicates whether or not the visitor is currently viewing the home page of your blog. The function is called “is_home.” Therefore, for the home page navigation item, you can use code like $class = (is_home()) ? 'active' : ''; to assign a class of “active” to your home navigation item.

Current Page

Now, if you have a bunch of pages (not posts) on your WordPress blog, you can use some code similar to the following to create a new link for each page and assign a class of “active” to the page that’s currently being viewed.

	foreach(get_pages('childof=0') as $p) {
		if($p->post_parent == 0) {
			$lc = ($p->ID == $wp_query->post->ID) ? ' class="active"' : '';
			echo '
				<li><a href="'.get_permalink($p->ID).'" title="'.$p->post_title.'"'.$lc.'>'.$p->post_title.'</a></li>';
		}
	}

In this case, we’re simply comparing the ID of the page for which we’re building the navigation item to the $wp_query->post->ID, which is the ID of the current page.

Because I was building a custom navigation menu, I used the get_pages() function; however, if you just want a pre-built navigation menu (complete with a class of “current_page_item” for the current item being viewed), you could simply use the wp_list_pages() function.

Current Category

Finally, if you’re building a navigation menu with a list of the categories, you can use code similar to the following.

	foreach(get_categories('exclude=1') as $p) {
		if($p->parent == 0) {
			$lc = (is_category($p->cat_name)) ? ' class="active"' : '';
			echo '
				<li><a href="'.get_bloginfo('url').'/'.$p->category_nicename.'" title="'.$p->cat_name.'"'.$lc.'>'.$p->cat_name.'</a></li>';
		}
	}

In this case, the is_category() function is the key. That function simply checks to see if the page currently being viewed is a category page. If you feed a category ID or category name into that function, WordPress checks to see if that’s the category page that’s currently being viewed.

Again, because I was building a custom navigation menu, I used the get_categories() function to retrieve a list of all of the categories. I chose to exclude the category with an ID of “1,” as the first category in a WordPress blog is always “Uncategorized,” which would not look very attractive in a navigation menu.

Did you like this post? Get monthly summary of our new tutorials, posts and tips to your inbox!

No Responses

Post Your Comment

Your email address will not be published.