Walker_Page

Core walker class used to create an HTML list of pages.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-walker-page.php  
  1. class Walker_Page extends Walker { 
  2.  
  3. /** 
  4. * What the class handles. 
  5. * @since 2.1.0 
  6. * @access public 
  7. * @var string 
  8. * @see Walker::$tree_type 
  9. */ 
  10. public $tree_type = 'page'; 
  11.  
  12. /** 
  13. * Database fields to use. 
  14. * @since 2.1.0 
  15. * @access private 
  16. * @var array 
  17. * @see Walker::$db_fields 
  18. * @todo Decouple this. 
  19. */ 
  20. public $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' ); 
  21.  
  22. /** 
  23. * Outputs the beginning of the current level in the tree before elements are output. 
  24. * @since 2.1.0 
  25. * @access public 
  26. * @see Walker::start_lvl() 
  27. * @param string $output Passed by reference. Used to append additional content. 
  28. * @param int $depth Optional. Depth of page. Used for padding. Default 0. 
  29. * @param array $args Optional. Arguments for outputting the next level. 
  30. * Default empty array. 
  31. */ 
  32. public function start_lvl( &$output, $depth = 0, $args = array() ) { 
  33. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) { 
  34. $t = "\t"; 
  35. $n = "\n"; 
  36. } else { 
  37. $t = ''; 
  38. $n = ''; 
  39. $indent = str_repeat( $t, $depth ); 
  40. $output .= "{$n}{$indent}<ul class='children'>{$n}"; 
  41.  
  42. /** 
  43. * Outputs the end of the current level in the tree after elements are output. 
  44. * @since 2.1.0 
  45. * @access public 
  46. * @see Walker::end_lvl() 
  47. * @param string $output Passed by reference. Used to append additional content. 
  48. * @param int $depth Optional. Depth of page. Used for padding. Default 0. 
  49. * @param array $args Optional. Arguments for outputting the end of the current level. 
  50. * Default empty array. 
  51. */ 
  52. public function end_lvl( &$output, $depth = 0, $args = array() ) { 
  53. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) { 
  54. $t = "\t"; 
  55. $n = "\n"; 
  56. } else { 
  57. $t = ''; 
  58. $n = ''; 
  59. $indent = str_repeat( $t, $depth ); 
  60. $output .= "{$indent}</ul>{$n}"; 
  61.  
  62. /** 
  63. * Outputs the beginning of the current element in the tree. 
  64. * @see Walker::start_el() 
  65. * @since 2.1.0 
  66. * @access public 
  67. * @param string $output Used to append additional content. Passed by reference. 
  68. * @param WP_Post $page Page data object. 
  69. * @param int $depth Optional. Depth of page. Used for padding. Default 0. 
  70. * @param array $args Optional. Array of arguments. Default empty array. 
  71. * @param int $current_page Optional. Page ID. Default 0. 
  72. */ 
  73. public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) { 
  74. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) { 
  75. $t = "\t"; 
  76. $n = "\n"; 
  77. } else { 
  78. $t = ''; 
  79. $n = ''; 
  80. if ( $depth ) { 
  81. $indent = str_repeat( $t, $depth ); 
  82. } else { 
  83. $indent = ''; 
  84.  
  85. $css_class = array( 'page_item', 'page-item-' . $page->ID ); 
  86.  
  87. if ( isset( $args['pages_with_children'][ $page->ID ] ) ) { 
  88. $css_class[] = 'page_item_has_children'; 
  89.  
  90. if ( ! empty( $current_page ) ) { 
  91. $_current_page = get_post( $current_page ); 
  92. if ( $_current_page && in_array( $page->ID, $_current_page->ancestors ) ) { 
  93. $css_class[] = 'current_page_ancestor'; 
  94. if ( $page->ID == $current_page ) { 
  95. $css_class[] = 'current_page_item'; 
  96. } elseif ( $_current_page && $page->ID == $_current_page->post_parent ) { 
  97. $css_class[] = 'current_page_parent'; 
  98. } elseif ( $page->ID == get_option('page_for_posts') ) { 
  99. $css_class[] = 'current_page_parent'; 
  100.  
  101. /** 
  102. * Filters the list of CSS classes to include with each page item in the list. 
  103. * @since 2.8.0 
  104. * @see wp_list_pages() 
  105. * @param array $css_class An array of CSS classes to be applied 
  106. * to each list item. 
  107. * @param WP_Post $page Page data object. 
  108. * @param int $depth Depth of page, used for padding. 
  109. * @param array $args An array of arguments. 
  110. * @param int $current_page ID of the current page. 
  111. */ 
  112. $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) ); 
  113.  
  114. if ( '' === $page->post_title ) { 
  115. /** translators: %d: ID of a post */ 
  116. $page->post_title = sprintf( __( '#%d (no title)' ), $page->ID ); 
  117.  
  118. $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before']; 
  119. $args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after']; 
  120.  
  121. $output .= $indent . sprintf( 
  122. '<li class="%s"><a href="%s">%s%s%s</a>',  
  123. $css_classes,  
  124. get_permalink( $page->ID ),  
  125. $args['link_before'],  
  126. /** This filter is documented in wp-includes/post-template.php */ 
  127. apply_filters( 'the_title', $page->post_title, $page->ID ),  
  128. $args['link_after'] 
  129. ); 
  130.  
  131. if ( ! empty( $args['show_date'] ) ) { 
  132. if ( 'modified' == $args['show_date'] ) { 
  133. $time = $page->post_modified; 
  134. } else { 
  135. $time = $page->post_date; 
  136.  
  137. $date_format = empty( $args['date_format'] ) ? '' : $args['date_format']; 
  138. $output .= " " . mysql2date( $date_format, $time ); 
  139.  
  140. /** 
  141. * Outputs the end of the current element in the tree. 
  142. * @since 2.1.0 
  143. * @access public 
  144. * @see Walker::end_el() 
  145. * @param string $output Used to append additional content. Passed by reference. 
  146. * @param WP_Post $page Page data object. Not used. 
  147. * @param int $depth Optional. Depth of page. Default 0 (unused). 
  148. * @param array $args Optional. Array of arguments. Default empty array. 
  149. */ 
  150. public function end_el( &$output, $page, $depth = 0, $args = array() ) { 
  151. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) { 
  152. $t = "\t"; 
  153. $n = "\n"; 
  154. } else { 
  155. $t = ''; 
  156. $n = ''; 
  157. $output .= "</li>{$n}"; 
  158.