wp_list_pages() function displays a list of WordPress pages as links and is often used to display navigation menus. When it prints the list of links, it adds a CSS class
current_page_item to the list item tag of the current page. This allows custom styling to be applied to the link e.g. to highlight a link to indicate the page that the visitor is on.
The limitation with this is that it only works if you are viewing a page or an attachment, or are on the posts page (as set in Settings > Reading).
I was working on an archive page for a custom taxonomy and wanted to have the
current_page_item CSS class added to the list item of a particular link of my choice. After poking around a bit in the
wp_list_pages() code listing, I discovered it’s possible to hack the
$wp_query object to make
wp_list_pages() do what I wanted it to.
// Get the ID of the link you want highlighted. Either: // - hardcode it (as below) // - or query the database to get it $the_id = "1"; // Make a copy of the $wp_query object $temp_query = clone $wp_query; // Trick WordPress into thinking we're on a page with ID $the_id $wp_query->is_page = 1; $wp_query->queried_object_id = $the_id; // Display pages wp_list_pages("title_li="); // Reset $wp_query $wp_query = clone $temp_query;
$wp_query->is_pageneeds to be set to
wp_list_pages()does not set a current page unless
- If you only want this code to run when you are on certain page you can wrap it in an
ifstatement that checks a WordPress conditional tag. In my case I only wanted to run this code when a taxonomy archive was being displayed so I used
- See http://www.jenst.se/2008/03/17/wordpress-get-id-by-post-or-page-name/ on how to query the database by post/page name to get an ID.