WordPress.org

Make WordPress Core

Ticket #4351: r-6666-classes.php.txt

File r-6666-classes.php.txt, 2.1 KB (added by ev3rywh3re, 6 years ago)

Changes to Walker_Page class to allow additional CSS for page root and branch

Line 
1Index: classes.php
2===================================================================
3--- classes.php (revision 6666)
4+++ classes.php (working copy)
5@@ -529,10 +529,18 @@
6 class Walker_Page extends Walker {
7        var $tree_type = 'page';
8        var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this
9+       var $marker_page_parent;
10+       var $_current_page;
11 
12-       function start_lvl($output, $depth) {
13+       function start_lvl($output, $depth, $current_page) {
14                $indent = str_repeat("\t", $depth);
15-               $output .= "\n$indent<ul>\n";
16+                               
17+               $this->page_marker( $current_page );
18+
19+               if ( $depth == 1 && $this->marker_page_parent != 0 ) $css_class .= ' current_page_root';
20+               if ( $css_class != '' ) $class = ' class="' . $css_class . '"';
21+               
22+               $output .= "\n$indent<ul$class>\n";
23                return $output;
24        }
25 
26@@ -550,12 +558,16 @@
27                       
28                extract($args, EXTR_SKIP);
29                $css_class = 'page_item page-item-'.$page->ID;
30-               $_current_page = get_page( $current_page );
31+               
32+               $this->page_marker( $current_page ); // Just in case start_lvl didn't invoke spirits
33+               
34                if ( $page->ID == $current_page )
35                        $css_class .= ' current_page_item ';
36-               elseif ( $_current_page && $page->ID == $_current_page->post_parent )
37+               elseif ( $this->_current_page && $page->ID == $this->marker_page_parent )
38                        $css_class .= ' current_page_parent';
39 
40+               if ( $depth == 1 && $this->marker_page_parent != 0 ) $css_class .= ' current_page_branch';
41+
42                $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page->ID) . '" title="' . attribute_escape(apply_filters('the_title', $page->post_title)) . '">' . apply_filters('the_title', $page->post_title) . '</a>';
43 
44                if ( !empty($show_date) ) {
45@@ -572,9 +584,18 @@
46 
47        function end_el($output, $page, $depth) {
48                $output .= "</li>\n";
49-
50                return $output;
51        }
52+       
53+       function page_marker( $current_page ) {
54+               if ( empty( $this->marker_page_parent ) ) {
55+                       $this->_current_page = get_page( $current_page );
56+                       $this->marker_page_parent = $this->_current_page->post_parent;
57+                       return $this->marker_page_parent;
58+               } else {
59+                       return $this->marker_page_parent;
60+               }
61+       }
62 
63 }
64