3 * Post API: Walker_Page class
11 * Core walker class used to create an HTML list of pages.
17 class Walker_Page extends Walker {
20 * What the class handles.
26 * @see Walker::$tree_type
28 public $tree_type = 'page';
31 * Database fields to use.
37 * @see Walker::$db_fields
38 * @todo Decouple this.
40 public $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' );
43 * Outputs the beginning of the current level in the tree before elements are output.
48 * @see Walker::start_lvl()
50 * @param string $output Passed by reference. Used to append additional content.
51 * @param int $depth Optional. Depth of page. Used for padding. Default 0.
52 * @param array $args Optional. Arguments for outputing the next level.
53 * Default empty array.
55 public function start_lvl( &$output, $depth = 0, $args = array() ) {
56 $indent = str_repeat("\t", $depth);
57 $output .= "\n$indent<ul class='children'>\n";
61 * Outputs the end of the current level in the tree after elements are output.
66 * @see Walker::end_lvl()
68 * @param string $output Passed by reference. Used to append additional content.
69 * @param int $depth Optional. Depth of page. Used for padding. Default 0.
70 * @param array $args Optional. Arguments for outputting the end of the current level.
71 * Default empty array.
73 public function end_lvl( &$output, $depth = 0, $args = array() ) {
74 $indent = str_repeat("\t", $depth);
75 $output .= "$indent</ul>\n";
79 * Outputs the beginning of the current element in the tree.
81 * @see Walker::start_el()
85 * @param string $output Used to append additional content. Passed by reference.
86 * @param WP_Post $page Page data object.
87 * @param int $depth Optional. Depth of page. Used for padding. Default 0.
88 * @param array $args Optional. Array of arguments. Default empty array.
89 * @param int $current_page Optional. Page ID. Default 0.
91 public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
93 $indent = str_repeat( "\t", $depth );
98 $css_class = array( 'page_item', 'page-item-' . $page->ID );
100 if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
101 $css_class[] = 'page_item_has_children';
104 if ( ! empty( $current_page ) ) {
105 $_current_page = get_post( $current_page );
106 if ( $_current_page && in_array( $page->ID, $_current_page->ancestors ) ) {
107 $css_class[] = 'current_page_ancestor';
109 if ( $page->ID == $current_page ) {
110 $css_class[] = 'current_page_item';
111 } elseif ( $_current_page && $page->ID == $_current_page->post_parent ) {
112 $css_class[] = 'current_page_parent';
114 } elseif ( $page->ID == get_option('page_for_posts') ) {
115 $css_class[] = 'current_page_parent';
119 * Filter the list of CSS classes to include with each page item in the list.
123 * @see wp_list_pages()
125 * @param array $css_class An array of CSS classes to be applied
127 * @param WP_Post $page Page data object.
128 * @param int $depth Depth of page, used for padding.
129 * @param array $args An array of arguments.
130 * @param int $current_page ID of the current page.
132 $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
134 if ( '' === $page->post_title ) {
135 /* translators: %d: ID of a post */
136 $page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
139 $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
140 $args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after'];
142 $output .= $indent . sprintf(
143 '<li class="%s"><a href="%s">%s%s%s</a>',
145 get_permalink( $page->ID ),
146 $args['link_before'],
147 /** This filter is documented in wp-includes/post-template.php */
148 apply_filters( 'the_title', $page->post_title, $page->ID ),
152 if ( ! empty( $args['show_date'] ) ) {
153 if ( 'modified' == $args['show_date'] ) {
154 $time = $page->post_modified;
156 $time = $page->post_date;
159 $date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
160 $output .= " " . mysql2date( $date_format, $time );
165 * Outputs the end of the current element in the tree.
170 * @see Walker::end_el()
172 * @param string $output Used to append additional content. Passed by reference.
173 * @param WP_Post $page Page data object. Not used.
174 * @param int $depth Optional. Depth of page. Default 0 (unused).
175 * @param array $args Optional. Array of arguments. Default empty array.
177 public function end_el( &$output, $page, $depth = 0, $args = array() ) {
178 $output .= "</li>\n";