12

WordPress hack: Force wp_list_pages() to print current_page_item class

Posted July 17th, 2010 in WordPress by Mark Leong

The 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.

Here’s how:

// 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;

Notes:

  • $wp_query->is_page needs to be set to 1 (true) because wp_list_pages() does not set a current page unless is_page() or is_attachment() or $wp_query->is_posts_page returns true.
  • If you only want this code to run when you are on certain page you can wrap it in an if statement 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 is_tax().
  • 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.

12 Responses so far.

  1. Short. Sweet. Powerful.. brilliant!

    Thanks :)

    Michael.

  2. David Bravo says:

    You’ve just avoided a nervious breakdown! It just works great. Thank you very much for your time and generosity. :)

  3. Martin says:

    very helpful, thank you

  4. Matt Stein says:

    Oh how awesome — thanks for posting this! Now for the beer I owe you: what’s your PayPal donation address?

  5. Thibault says:

    Thanks, it works just fine.

  6. Cyrille says:

    Thanks! it made my day!

  7. Matt says:

    To make this work for an archive page, you also need a $wp_query->queried_object = ”; under line 10.

  8. Coliq says:

    Your idea is brilliant, but i don’t know why i can’t implement it.
    but i found another way with simple code.

    just call wp_list_pages() before call query or else
    best case is after get_header();

    Thanks!

  9. Coliq says:

    sorry, just left important note.

    call wp_list_pages using echo=0, and save to variable.
    $somevar = wp_list_pages(‘title_li&echo=0′);

  10. Robert says:

    Thanks a lot for that Post!

  11. Richard says:

    Awesome mate, thanks for sharing this. Just what I was looking for and saved me some time!

Leave a Reply