wordpress secondary navigation


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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. echo implode( "
  62. ", $this->output_lines );
  63. }
  64.  
  65. /**
  66.   * Check if current nav item in the loop is the current page, or an ancestor of it.
  67.   * @param Int $post_id
  68.   * @return bool
  69.   */
  70. function is_current( $post_id )
  71. {
  72. return ( in_array( $post_id, $this->current_post->ancestors ) || $this->current_post->ID === $post_id );
  73. }
  74.  
  75. /**
  76.   * 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
  77.   * @param Object $page
  78.   * @return bool
  79.   */
  80. function level( $page )
  81. {
  82. // Get all second level pages in this section
  83. $pages = $this->get_child_pages( $page );
  84.  
  85. // Check if there are any sub pages
  86. if( !count($pages) ){
  87. return false;
  88. }
  89.  
  90. $this->output_lines[] = '<ul id="' . $this->get_ul_id( $this->depth ) . '">';
  91.  
  92. // Loop pages and make menu items
  93. foreach( $pages as $item ){
  94. if ( $this->is_current( $item->ID ) ) {
  95. // Current second level page
  96. $this->output_lines[] = sprintf(
  97. '<li class="%s"><a href="%s">%s</a>',
  98. $this->current_class,
  99. get_permalink( $item->ID ),
  100. $item->post_title
  101. );
  102. // Check for Sub Pages, and display sub list
  103. $this->level( $item->ID );
  104.  
  105.  
  106. $this->output_lines[] = '</li>';
  107. }else{
  108. // normal list items, not current, no subnav
  109. $this->output_lines[] = sprintf( '<li><a href="%s">%s</a></li>', get_permalink( $item->ID ), $item->post_title );
  110. }
  111. }
  112.  
  113. $this->output_lines[] = '</ul>';
  114.  
  115. $this->depth ++;
  116. return true;
  117. }
  118.  
  119. function get_ul_id( $level )
  120. {
  121. switch( $level ){
  122. case 2:
  123. return $this->nav_id;
  124. break;
  125. case 3:
  126. return 'subNav';
  127. break;
  128. default:
  129. return 'subNav_' . $level;
  130. break;
  131. }
  132. }
  133.  
  134. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.