BE_Genesis_Subpages_Menu

Plugin Name: Genesis Subpages as Secondary Menu Plugin URI: https://github.com/billerickson/Genesis-Subpages-as-Secondary-Menu Description: Replaces the Genesis Secondary Menu with a dynamic listing of the current section's subpages.

Defined (1)

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

/genesis-subpages-as-secondary-menu.php  
  1. class BE_Genesis_Subpages_Menu { 
  2.  
  3. var $instance; 
  4.  
  5. /** 
  6. * Construct 
  7. * Registers our activation hook and init hook 
  8. * @since 2.0 
  9. * @author Bill Erickson 
  10. */ 
  11. function __construct() { 
  12. $this->instance =& $this; 
  13. register_activation_hook( __FILE__, array( $this, 'activation_hook' ) ); 
  14. add_action( 'init', array( $this, 'init' ) );  
  15.  
  16. /** 
  17. * Activation Hook 
  18. * Confirm site is using Genesis 
  19. * @since 2.0 
  20. * @author Bill Erickson 
  21. */ 
  22. function activation_hook() { 
  23. if ( 'genesis' != basename( TEMPLATEPATH ) ) { 
  24. deactivate_plugins( plugin_basename( __FILE__ ) ); 
  25. wp_die( sprintf( __( 'Sorry, you cannot activate unless you have installed <a href="%s">Genesis</a>', 'genesis-grid'), 'http://www.billerickson.net/go/genesis' ) ); 
  26.  
  27. /** 
  28. * Init 
  29. * Register all our functions to the appropriate hook 
  30. * @since 2.0 
  31. * @author Bill Erickson 
  32. */ 
  33. function init() { 
  34.  
  35. // Indicate the Secondary Menu location is in use 
  36. add_filter( 'theme_mod_nav_menu_locations', array( $this, 'subnav_menu_location' ) ); 
  37.  
  38. // Build the Dynamic Menu Object 
  39. add_filter( 'wp_get_nav_menu_object', array( $this, 'subnav_menu_object' ), 10, 2 ); 
  40.  
  41. // Mark the Secondary Menu location as having a menu 
  42. add_filter( 'has_nav_menu', array( $this, 'subnav_menu_has_menu' ), 10, 2 ); 
  43.  
  44. // Short circuit wp_nav_menu() for Secondary Menu 
  45. add_filter( 'pre_wp_nav_menu', array( $this, 'be_pre_subnav' ), 10, 2 ); 
  46.  
  47. // Add menu-item class  
  48. add_filter( 'page_css_class', array( $this, 'subnav_item_classes' ) ); 
  49.  
  50. // Output Secondary Menu 
  51. add_filter('genesis_do_subnav', array( $this, 'subnav_output' ) ); 
  52.  
  53.  
  54. /** 
  55. * Indicate the Secondary Menu location is in use 
  56. * @param array $locations, menu locations and the menu ID assigned to that location 
  57. * @return array $locations 
  58. * @since 2.0 
  59. * @author Bill Erickson 
  60. */ 
  61. function subnav_menu_location( $locations ) { 
  62.  
  63. $locations['secondary'] = 1; 
  64. return $locations; 
  65.  
  66.  
  67. /** 
  68. * Dynamic Menu Object 
  69. * @param object $menu_object 
  70. * @param int $menu_id 
  71. * @return object $menu_object 
  72. * @since 2.0 
  73. * @author Bill Erickson 
  74. */ 
  75. function subnav_menu_object( $menu_object, $menu_id ) { 
  76.  
  77. if( 1 === $menu_id ) { 
  78. $menu_object = new stdClass(); 
  79. $menu_object->name = 'Genesis Subpages'; 
  80. $menu_object->term_id = 1; 
  81. $menu_object->slug = 'genesis-subpages-as-secondary-menu';  
  82.  
  83. return $menu_object; 
  84.  
  85.  
  86. /** 
  87. * Mark Secondary Menu location as having a menu 
  88. * @param bool $has_nav_menu 
  89. * @param string $location 
  90. * @return bool $has_nav_menu 
  91. * @since 2.0 
  92. * @author Bill Erickson 
  93. */ 
  94. function subnav_menu_has_menu( $has_nav_menu, $location ) { 
  95.  
  96. if( 'secondary' == $location ) 
  97. $has_nav_menu = true; 
  98. return $has_nav_menu; 
  99.  
  100.  
  101. /** 
  102. * Short Circuit wp_nav_menu() for secondary menu 
  103. * @param string $output, output of wp_nav_menu (I'm overriding this later) 
  104. * @param object $args, wp_nav_menu arguments 
  105. * @return string $output 
  106. * @since 2.0 
  107. * @author Bill Erickson 
  108. */ 
  109. function be_pre_subnav( $output, $args ) { 
  110.  
  111. if( 'secondary' == $args->theme_location ) 
  112. $output = 1; 
  113. return $output; 
  114.  
  115.  
  116. /** 
  117. * Add menu-item class 
  118. * @param array $classes 
  119. * @return array $classes 
  120. * @since 2.0 
  121. * @author Bill Erickson 
  122. */ 
  123. function subnav_item_classes( $classes ) { 
  124. $classes[] = 'menu-item'; 
  125. return $classes; 
  126.  
  127. /** 
  128. * Display Secondary Nav with Subpages 
  129. * @param string $output, output of genesis_do_subnav() 
  130. * @return string $output 
  131. * @since 2.0 
  132. * @author Bill Erickson 
  133. */ 
  134. function subnav_output( $output ) { 
  135.  
  136. // Only run on pages 
  137. if( ! is_page() ) 
  138. return; 
  139.  
  140. // Find top level parent 
  141. $parents = array_reverse( get_ancestors( get_the_ID(), 'page' ) ); 
  142. $parents[] = get_the_ID(); 
  143.  
  144. // Build a menu listing top level parent's children 
  145. $args = array( 
  146. 'child_of' => $parents[0],  
  147. 'title_li' => '',  
  148. 'echo' => false,  
  149. ); 
  150. $subnav = wp_list_pages( apply_filters( 'be_genesis_subpages_args', $args ) ); 
  151. if( empty( $subnav ) ) 
  152. return; 
  153.  
  154. // Wrap the list items in an unordered list 
  155. $wrapper = apply_filters( 'be_genesis_subpages_wrapper', array( '<ul id="menu-genesis-subpages" class="nav genesis-nav-menu menu-secondary">', '</ul>' ) ); 
  156. $subnav = $wrapper[0] . $subnav . $wrapper[1]; 
  157.  
  158. $subnav_markup_open = genesis_markup( array( 
  159. 'html5' => '<nav %s>',  
  160. 'xhtml' => '<div id="subnav">',  
  161. 'context' => 'nav-secondary',  
  162. 'echo' => false,  
  163. ) ); 
  164. $subnav_markup_open .= genesis_structural_wrap( 'menu-secondary', 'open', 0 ); 
  165.  
  166. $subnav_markup_close = genesis_structural_wrap( 'menu-secondary', 'close', 0 ); 
  167. $subnav_markup_close .= genesis_html5() ? '</nav>' : '</div>'; 
  168.  
  169. $output = $subnav_markup_open . $subnav . $subnav_markup_close; 
  170. return $output;  
  171.  
  172.