WordPress.org

Make WordPress Core

#21525 closed defect (bug) (duplicate)

wp_list_pages: current page css fails with custom post types

Reported by: martin.czerwi Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.4.1
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description

In a customer project I used a custom post type 'products' for content-pages. 'products' is registered with 'public' => true and 'hierarchical' => true.

For a sub navigation I'm using wp_list_pages() to list all sibling 'products'. Usually you get css-classes in the output, to highlight the current pages, or ancestors and so on. This works fine, when using regular pages, but when using my custom post type, the current page isn't highlighted, the class is missing.

I looked at the code and after testing a bit I discovered walk_page_tree() receives '0' as the $current_page parameter. That's the reason why no css-classes for the current page are output.

In wp-includes/post-template.php in wp_list_pages() (http://core.trac.wordpress.org/browser/trunk/wp-includes/post-template.php#L828) the $current_page parameter for walk_page_tree() is determined. When you have a custom post-type the if statement fails:

if ( is_page() || is_attachment() || $wp_query->is_posts_page )
    $current_page = $wp_query->get_queried_object_id();

Change History (2)

comment:1 martin.czerwi21 months ago

As an addition, when modifying the core file wp-includes/post-template.php with a check like 'is_post_type_hierarchical', everything worked as expected. My final code looks like this:

if ( is_page() || is_attachment() || $wp_query->is_posts_page || is_post_type_hierarchical( $wp_query->query['post_type'] ) )
    $current_page = $wp_query->get_queried_object_id();

Maybe it's even possible to leave out is_page() in the statement. I didn't try that.

comment:2 SergeyBiryukov21 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.