diff --git a/wp-includes/classes.php b/wp-includes/classes.php index 80c51ae14a..9efe55ee8c 100644 --- a/wp-includes/classes.php +++ b/wp-includes/classes.php @@ -551,8 +551,10 @@ class Walker_Page extends Walker { extract($args, EXTR_SKIP); $css_class = 'page_item page-item-'.$page->ID; $_current_page = get_page( $current_page ); + if ( in_array($page->ID, $_current_page->ancestors) ) + $css_class .= ' current_page_ancestor'; if ( $page->ID == $current_page ) - $css_class .= ' current_page_item '; + $css_class .= ' current_page_item'; elseif ( $_current_page && $page->ID == $_current_page->post_parent ) $css_class .= ' current_page_parent'; diff --git a/wp-includes/post.php b/wp-includes/post.php index edfb412ab7..f3d0ca0869 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -175,6 +175,10 @@ function &get_post(&$post, $output = OBJECT, $filter = 'raw') { } } + // Populate the ancestors field. + // Not cached since we don't clear cache for ancestors when a post changes. + _get_post_ancestors($_post); + $_post = sanitize_post($_post, $filter); if ( $output == OBJECT ) { @@ -190,6 +194,26 @@ function &get_post(&$post, $output = OBJECT, $filter = 'raw') { } } +/** + * get_post_ancestors() - Retrieve ancestors for a post + * + * @package WordPress + * @subpackage Post + * @since 2.5 + * + * @param string $field {@internal Missing Description}} + * @param int|object &$post post ID or post object + * @return array of ancestor IDs + */ +function get_post_ancestors($post) { + $post = get_post(); + + if ( !empty($post->ancestors) ) + return $post->ancestors; + + return array(); +} + /** * get_post_field() - Retrieve a field based on a post ID. * @@ -2884,4 +2908,27 @@ function _save_post_hook($post_id, $post) { } } +// +// Private +// + +function _get_post_ancestors(&$_post) { + global $wpdb; + + if ( !empty($_post->ancestors) ) + return; + + $_post->ancestors = array(); + + if ( empty($_post->post_parent) || $_post->ID == $_post->post_parent ) + return; + + $id = $_post->ancestors[] = $_post->post_parent; + while ( $ancestor = $wpdb->get_var("SELECT `post_parent` FROM $wpdb->posts WHERE ID= '{$id}' LIMIT 1") ) { + if ( $id == $ancestor ) + break; + $id = $_post->ancestors[] = $ancestor; + } +} + ?>