Make WordPress Core


Ignore:
Timestamp:
02/27/2008 11:28:18 PM (17 years ago)
Author:
ryan
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r7045 r7074  
    176176    }
    177177
     178    // Populate the ancestors field.
     179    // Not cached since we don't clear cache for ancestors when a post changes.
     180    _get_post_ancestors($_post);
     181
    178182    $_post = sanitize_post($_post, $filter);
    179183
     
    189193        return $_post;
    190194    }
     195}
     196
     197/**
     198 * get_post_ancestors() - Retrieve ancestors for a post
     199 *
     200 * @package WordPress
     201 * @subpackage Post
     202 * @since 2.5
     203 *
     204 * @param string $field {@internal Missing Description}}
     205 * @param int|object &$post post ID or post object
     206 * @return array of ancestor IDs
     207 */
     208function get_post_ancestors($post) {
     209    $post = get_post();
     210
     211    if ( !empty($post->ancestors) )
     212        return $post->ancestors;
     213
     214    return array();
    191215}
    192216
     
    28852909}
    28862910
     2911//
     2912// Private
     2913//
     2914
     2915function _get_post_ancestors(&$_post) {
     2916    global $wpdb;
     2917
     2918    if ( !empty($_post->ancestors) )
     2919        return;
     2920 
     2921    $_post->ancestors = array();
     2922
     2923    if ( empty($_post->post_parent) || $_post->ID == $_post->post_parent )
     2924        return;
     2925
     2926    $id = $_post->ancestors[] = $_post->post_parent;
     2927    while ( $ancestor = $wpdb->get_var("SELECT `post_parent` FROM $wpdb->posts WHERE ID= '{$id}' LIMIT 1") ) {
     2928        if ( $id == $ancestor )
     2929            break;
     2930        $id = $_post->ancestors[] = $ancestor;
     2931    }
     2932}
     2933
    28872934?>
Note: See TracChangeset for help on using the changeset viewer.