Introduce get_post_ancestors(). Add current_page_ancestor class to ancestors of the current page. Props AaronCampbell. fixes #5662

git-svn-id: https://develop.svn.wordpress.org/trunk@7074 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2008-02-27 23:28:18 +00:00
parent 75512587f1
commit 0808c1b9a1
2 changed files with 50 additions and 1 deletions

View File

@ -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';

View File

@ -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;
}
}
?>