Opened 13 years ago
Closed 11 years ago
#17590 closed defect (bug) (fixed)
wp_list_pages() not setting "current_page_item" classes on custom post types
Reported by: | tobiasn | Owned by: | wonderboymusic |
---|---|---|---|
Milestone: | 3.9 | Priority: | normal |
Severity: | normal | Version: | 3.1 |
Component: | Posts, Post Types | Keywords: | has-patch commit |
Focuses: | Cc: |
Description (last modified by )
wp_list_pages() don't set the correct css for a hierarchical custom post type.
To reproduce this bug do this:
<?php wp_list_pages("title_li=&post_type=custom_post_type_name"); ?>
and compare the html output to normal pages:
<?php wp_list_pages(); ?>
The problem seems to be this row in wp_list_pages():
if ( is_page() || is_attachment() || $wp_query->is_posts_page )
In the following patch i've changed it to
if ( is_page() || is_attachment() || $wp_query->is_posts_page || ($wp_query->query_vars['post_type'] != 'post' && is_single()) )
(If this is not the intended behavoir please let me know. )
Attachments (6)
Change History (27)
#1
follow-up:
↓ 3
@
13 years ago
- Cc ben@… added
As a temporary measure you can hook into to add selected classes like this:
function my_page_css_class( $css_class, $page ) { global $post; if ( $post->ID == $page->ID ) { $css_class[] = 'current_page_item'; } return $css_class; } add_filter( 'page_css_class', 'my_page_css_class', 10, 2 );
#3
in reply to:
↑ 1
@
13 years ago
Nice quickfix, however i would prefer pinpointing custom post types.
Replying to husobj:
As a temporary measure you can hook into to add selected classes like this:
function my_page_css_class( $css_class, $page ) { global $post; if ( $post->ID == $page->ID ) { $css_class[] = 'current_page_item'; } return $css_class; } add_filter( 'page_css_class', 'my_page_css_class', 10, 2 );
#4
@
13 years ago
Thanks for the fix with the hook guys.
However I really wonder why it is still not fixed after 9 months.
#5
@
13 years ago
- Cc daveleeone added
- Type changed from defect (bug) to feature request
Thanks for the very useful hook, but how can I highlight the parent page? At the moment I use Jquery for this.
Any Ideas?
Thanks, Davelee
#7
@
13 years ago
Hi dave! I haven't noticed the parent page-thing, guess that's a bug in the Walker class. How do I recreate it?
#8
follow-up:
↓ 9
@
13 years ago
Hi Tobias, thanks for reply.
I have not seen, unfortunately, you've written back to me.
here is my code in the template:
function file:
function my_page_css_class( $css_class, $page ) { global $post; if ( $post->ID == $page->ID ) { $css_class[] = 'current_page_item'; } return $css_class; } add_filter( 'page_css_class', 'my_page_css_class', 10, 2 );
template file:
<?php // create a ID of parent page to use the wp_list_page as submenu if ($post->post_parent) { $ancestors=get_post_ancestors($post->ID); $root=count($ancestors)-1; $parent = $ancestors[$root]; } else { $parent = $post->ID; } ?> <ul class="submenu"> <?php // create a Submenu for CPT's $args = array( 'post_type' => 'event', 'child_of' => $parent, 'title_li' => __('') ); wp_list_pages( $args ); ?> </ul>
jquery file
$(document).ready(function(){ $('.submenu li:has(li.current_page_item)').addClass('parent_page_item'); });
I hope it helps.
if you make it I'll pay you a beer :)
#9
in reply to:
↑ 8
@
13 years ago
Hi Dave! This has nothing to do with the trac bug. Could you please remove the comment here and repost it in the support forum? I'll answer you there.
#10
@
12 years ago
Hi,
I was wondering if this had been looked at ?
it seems like this would be quite useful now that CPT are used so often by the wp community
thanks
#13
@
12 years ago
17590.2.patch does a hierarchical check, as suggested during the bug scrub.
is_attachment()
was added in [10947] (for #9472), so I've kept it.
#16
follow-up:
↓ 17
@
11 years ago
Patch looks good to me. Anything I can do to help this get in core? Just ran into this issue myself.
#17
in reply to:
↑ 16
@
11 years ago
I think @obenland patch works perfect and addresses the @nacin issue but I don't really know how to get this fix into 3.9. Any suggestions? :)
Replying to billerickson:
Patch looks good to me. Anything I can do to help this get in core? Just ran into this issue myself.
#19
@
11 years ago
- Component changed from Themes to Post Types
- Keywords commit added
Looks good. The is_singular() should ensure this is checking a single post type.
#20
@
11 years ago
17590.diff is a slightly more defensive approach. get_post_type() simply returns the post type query variable, which could very well have been changed to an array on pre_get_posts even after is_singular was set in parse_query(). Instead, we can use the queried object, which isn't foolproof either but is at least it's consistent for what we're doing here.
wp_list_pages() css patch for custom post types