Make WordPress Core


Ignore:
Timestamp:
12/30/2004 07:41:14 PM (20 years ago)
Author:
rboren
Message:

Add get_pages(). Improve wp_list_pages(). Props: Adi Sieker and Chris Waigl.

File:
1 edited

Legend:

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

    r1976 r2022  
    322322//
    323323
    324 function wp_list_pages($args = '') {
     324function get_pages($args = '') {
    325325    global $wpdb;
    326326
    327327    parse_str($args, $r);
     328
    328329    if (!isset($r['child_of'])) $r['child_of'] = 0;
    329     if (!isset($r['depth'])) $r['depth'] = 0;
    330     if (!isset($r['show_date'])) $r['show_date'] = '';
    331330    if (!isset($r['sort_column'])) $r['sort_column'] = 'post_title';
    332331    if (!isset($r['sort_order'])) $r['sort_order'] = 'ASC';
    333 
    334332
    335333    $exclusions = '';
     
    343341    }
    344342
    345     $option_dates = '';
    346     if (! empty($r['show_date'])) {
    347         if ('modified' == $r['show_date'])
    348             $option_dates = ",UNIX_TIMESTAMP(post_modified) AS ts";
    349         else
    350             $option_dates = ",UNIX_TIMESTAMP(post_date) AS ts";
    351     }
     343    $dates = ",UNIX_TIMESTAMP(post_modified) AS time_modified";
     344    $dates .= ",UNIX_TIMESTAMP(post_date) AS time_created";
    352345
    353346    $post_parent = '';
     
    355348        $post_parent = ' AND post_parent=' . $r['child_of'] . ' ';
    356349    }
     350
    357351    $pages = $wpdb->get_results("SELECT " .
    358352                                                            "ID, post_title,post_parent " .
    359                                                             "$option_dates " .
     353                                                            "$dates " .
    360354                                                            "FROM $wpdb->posts " .
    361355                                                            "WHERE post_status = 'static' " .
     
    363357                                                            "$exclusions " .
    364358                                                            "ORDER BY " . $r['sort_column'] . " " . $r['sort_order']);
     359
     360    return $pages;
     361}
     362
     363function wp_list_pages($args = '') {
     364    parse_str($args, $r);
     365    if (!isset($r['depth'])) $r['depth'] = 0;
     366    if (!isset($r['show_date'])) $r['show_date'] = '';
     367    if (!isset($r['child_of'])) $r['child_of'] = 0;
     368
     369    // Query pages.
     370    $pages = get_pages($args);
     371
     372    // Now loop over all pages that were selected
    365373    $page_tree = Array();
    366374    foreach($pages as $page) {
     375        // set the title for the current page
    367376        $page_tree[$page->ID]['title'] = $page->post_title;
    368377
    369         if(!empty($r['show_date'])) {
    370             $page_tree[$page->ID]['ts'] = $page->ts;
    371         }
    372         $page_tree[$page->post_parent]['children'][] = $page->ID;
    373     }
    374     page_level_out($r['child_of'],$page_tree, $r);
    375 }
    376 
    377 function page_level_out($parent, $page_tree, $args, $depth = 0) {
     378        // set the selected date for the current page
     379        // depending on the query arguments this is either
     380        // the createtion date or the modification date
     381        // as a unix timestamp. It will also always be in the
     382        // ts field.
     383        if (! empty($r['show_date'])) {
     384            if ('modified' == $r['show_date'])
     385                $page_tree[$page->ID]['ts'] = $page->date_modified;
     386            else
     387                $page_tree[$page->ID]['ts'] = $page->date_created;             
     388        }
     389
     390        // The tricky bit!!
     391        // Using the parent ID of the current page as the
     392        // array index we set the curent page as a child of that page.
     393        // We can now start looping over the $page_tree array
     394        // with any ID which will output the page links from that ID downwards.
     395        $page_tree[$page->post_parent]['children'][] = $page->ID;   
     396    }
     397    // Output of the pages starting with child_of as the root ID.
     398    // child_of defaults to 0 if not supplied in the query.
     399    _page_level_out($r['child_of'],$page_tree, $r);
     400}
     401
     402function _page_level_out($parent, $page_tree, $args, $depth = 0) {
     403    global $wp_query;
     404
     405    $queried_obj = $wp_query->get_queried_object();
     406
    378407    if($depth)
    379408        $indent = join(array_fill(0,$depth,"\t"));
     
    382411        $cur_page = $page_tree[$page_id];
    383412        $title = $cur_page['title'];
    384         echo $indent . '<li><a href="' . get_page_link($page_id) . '" title="' . wp_specialchars($title) . '">' . $title . '</a>';
     413
     414        $css_class = 'page_item';
     415        if( $page_id == $queried_obj->ID) {
     416            $css_class .= ' current_page_item';
     417        }
     418
     419        echo $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page_id) . '" title="' . wp_specialchars($title) . '">' . $title . '</a>';
     420
    385421        if(isset($cur_page['ts'])) {
    386422            $format = get_settings('date_format');
     
    396432
    397433            if(!$args['depth'] || $depth < ($args['depth']-1)) {
    398                 page_level_out($page_id,$page_tree, $args, $new_depth);
     434                _page_level_out($page_id,$page_tree, $args, $new_depth);
    399435            }
    400436            echo "$indent</ul>\n";
    401437        }
    402438        echo "$indent</li>\n";
    403 
    404439    }
    405440}
Note: See TracChangeset for help on using the changeset viewer.