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 outputting the next level.
53 * Default empty array.
55 public function start_lvl( &$output, $depth = 0, $args = array() ) {
56 if ( 'preserve' === $args['item_spacing'] ) {
63 $indent = str_repeat( $t, $depth );
64 $output .= "{$n}{$indent}<ul class='children'>{$n}";
68 * Outputs the end of the current level in the tree after elements are output.
73 * @see Walker::end_lvl()
75 * @param string $output Passed by reference. Used to append additional content.
76 * @param int $depth Optional. Depth of page. Used for padding. Default 0.
77 * @param array $args Optional. Arguments for outputting the end of the current level.
78 * Default empty array.
80 public function end_lvl( &$output, $depth = 0, $args = array() ) {
81 if ( 'preserve' === $args['item_spacing'] ) {
88 $indent = str_repeat( $t, $depth );
89 $output .= "{$indent}</ul>{$n}";
93 * Outputs the beginning of the current element in the tree.
95 * @see Walker::start_el()
99 * @param string $output Used to append additional content. Passed by reference.
100 * @param WP_Post $page Page data object.
101 * @param int $depth Optional. Depth of page. Used for padding. Default 0.
102 * @param array $args Optional. Array of arguments. Default empty array.
103 * @param int $current_page Optional. Page ID. Default 0.
105 public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
106 if ( 'preserve' === $args['item_spacing'] ) {
114 $indent = str_repeat( $t, $depth );
119 $css_class = array( 'page_item', 'page-item-' . $page->ID );
121 if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
122 $css_class[] = 'page_item_has_children';
125 if ( ! empty( $current_page ) ) {
126 $_current_page = get_post( $current_page );
127 if ( $_current_page && in_array( $page->ID, $_current_page->ancestors ) ) {
128 $css_class[] = 'current_page_ancestor';
130 if ( $page->ID == $current_page ) {
131 $css_class[] = 'current_page_item';
132 } elseif ( $_current_page && $page->ID == $_current_page->post_parent ) {
133 $css_class[] = 'current_page_parent';
135 } elseif ( $page->ID == get_option('page_for_posts') ) {
136 $css_class[] = 'current_page_parent';
140 * Filters the list of CSS classes to include with each page item in the list.
144 * @see wp_list_pages()
146 * @param array $css_class An array of CSS classes to be applied
148 * @param WP_Post $page Page data object.
149 * @param int $depth Depth of page, used for padding.
150 * @param array $args An array of arguments.
151 * @param int $current_page ID of the current page.
153 $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
155 if ( '' === $page->post_title ) {
156 /* translators: %d: ID of a post */
157 $page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
160 $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
161 $args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after'];
163 $output .= $indent . sprintf(
164 '<li class="%s"><a href="%s">%s%s%s</a>',
166 get_permalink( $page->ID ),
167 $args['link_before'],
168 /** This filter is documented in wp-includes/post-template.php */
169 apply_filters( 'the_title', $page->post_title, $page->ID ),
173 if ( ! empty( $args['show_date'] ) ) {
174 if ( 'modified' == $args['show_date'] ) {
175 $time = $page->post_modified;
177 $time = $page->post_date;
180 $date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
181 $output .= " " . mysql2date( $date_format, $time );
186 * Outputs the end of the current element in the tree.
191 * @see Walker::end_el()
193 * @param string $output Used to append additional content. Passed by reference.
194 * @param WP_Post $page Page data object. Not used.
195 * @param int $depth Optional. Depth of page. Default 0 (unused).
196 * @param array $args Optional. Array of arguments. Default empty array.
198 public function end_el( &$output, $page, $depth = 0, $args = array() ) {
199 if ( 'preserve' === $args['item_spacing'] ) {
206 $output .= "</li>{$n}";