Posted By

errkk on 08/10/11


Tagged

pages wordpress navigation nav subnav


Versions (?)

wordpress secondary navigation


 / Published in: PHP
 

  1. class NavSec
  2. {
  3. private $current_post = 0;
  4. private $top_ancestor = 0;
  5. private $depth = 2;
  6. private $output_lines = array();
  7. private $current_class = 'current';
  8. private $nav_id = 'sideNav';
  9.  
  10. public function __construct( $current_post )
  11. {
  12. $this->current_post = $current_post;
  13. $this->top_ancestor = $this->get_top_level_ancestor( $this->current_post->ID );
  14.  
  15.  
  16. }
  17.  
  18.  
  19. /**
  20.   * Find the page that is the highest ancestor of the current page. Normally a section page from the primary Nav
  21.   * @param int $post_id
  22.   * @return Object
  23.   */
  24. function get_top_level_ancestor( $post_id )
  25. {
  26. $tmp = get_page( $id );
  27. while ( intVal( $tmp->post_parent ) > 0 ) {
  28. $tmp = get_page( $tmp->post_parent );
  29. }
  30. return $tmp;
  31. }
  32.  
  33. /**
  34.   * Get 1 level of child pages for the supplied post ID
  35.   * @param int $post_id
  36.   * @return array
  37.   */
  38. function get_child_pages( $post_id )
  39. {
  40. return get_pages( array(
  41. 'child_of' => $post_id,
  42. 'parent' => $post_id,
  43. 'sort_order' => 'ASC',
  44. 'sort_column' => 'menu_order',
  45. 'hierarchical' => 0
  46. ) );
  47. }
  48.  
  49. /**
  50.   * Output the HTML of the lists
  51.   */
  52. function render()
  53. {
  54. // Find top section page
  55. $section_post = get_post( $this->top_ancestor );
  56.  
  57. // Loop thru secondary level pages
  58. $this->level( $this->top_ancestor->ID );
  59.  
  60. // output the lines that have been made
  61. ", $this->output_lines );
  62. }
  63.  
  64. /**
  65.   * Check if current nav item in the loop is the current page, or an ancestor of it.
  66.   * @param Int $post_id
  67.   * @return bool
  68.   */
  69. function is_current( $post_id )
  70. {
  71. return ( in_array( $post_id, $this->current_post->ancestors ) || $this->current_post->ID === $post_id );
  72. }
  73.  
  74. /**
  75.   * Loop thru current level of pages. For the current item, check if there are sub pages, and call this function again inside to do that level
  76.   * @param Object $page
  77.   * @return bool
  78.   */
  79. function level( $page )
  80. {
  81. // Get all second level pages in this section
  82. $pages = $this->get_child_pages( $page );
  83.  
  84. // Check if there are any sub pages
  85. if( !count($pages) ){
  86. return false;
  87. }
  88.  
  89. $this->output_lines[] = '<ul id="' . $this->get_ul_id( $this->depth ) . '">';
  90.  
  91. // Loop pages and make menu items
  92. foreach( $pages as $item ){
  93. if ( $this->is_current( $item->ID ) ) {
  94. // Current second level page
  95. $this->output_lines[] = sprintf(
  96. '<li class="%s"><a href="%s">%s</a>',
  97. $this->current_class,
  98. get_permalink( $item->ID ),
  99. $item->post_title
  100. );
  101. // Check for Sub Pages, and display sub list
  102. $this->level( $item->ID );
  103.  
  104.  
  105. $this->output_lines[] = '</li>';
  106. }else{
  107. // normal list items, not current, no subnav
  108. $this->output_lines[] = sprintf( '<li><a href="%s">%s</a></li>', get_permalink( $item->ID ), $item->post_title );
  109. }
  110. }
  111.  
  112. $this->output_lines[] = '</ul>';
  113.  
  114. $this->depth ++;
  115. return true;
  116. }
  117.  
  118. function get_ul_id( $level )
  119. {
  120. switch( $level ){
  121. case 2:
  122. return $this->nav_id;
  123. break;
  124. case 3:
  125. return 'subNav';
  126. break;
  127. default:
  128. return 'subNav_' . $level;
  129. break;
  130. }
  131. }
  132.  
  133. }

Report this snippet  

You need to login to post a comment.