Wordpress Selective page Hierarchy


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

Originally from:
http://wordpress.mfields.org/2010/selective-page-hierarchy-for-wp_list_pages/


Copy this code and paste it in your HTML
  1. funtions.php:
  2. /**
  3. * Create HTML list of pages.
  4. *
  5. * @package Razorback
  6. * @subpackage Walker
  7. * @author Michael Fields <[email protected]>
  8. * @copyright Copyright (c) 2010, Michael Fields
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. *
  11. * @uses Walker_Page
  12. *
  13. * @since 2010-05-28
  14. * @alter 2010-10-09
  15. */
  16. class Razorback_Walker_Page_Selective_Children extends Walker_Page {
  17. /**
  18. * Walk the Page Tree.
  19. *
  20. * @global stdClass WordPress post object.
  21. * @uses Walker_Page::$db_fields
  22. * @uses Walker_Page::display_element()
  23. *
  24. * @since 2010-05-28
  25. * @alter 2010-10-09
  26. */
  27. function walk( $elements, $max_depth ) {
  28. global $post;
  29. $args = array_slice( func_get_args(), 2 );
  30. $output = '';
  31.  
  32. /* invalid parameter */
  33. if ( $max_depth < -1 ) {
  34. return $output;
  35. }
  36.  
  37. /* Nothing to walk */
  38. if ( empty( $elements ) ) {
  39. return $output;
  40. }
  41.  
  42. /* Set up variables. */
  43. $top_level_elements = array();
  44. $children_elements = array();
  45. $parent_field = $this->db_fields['parent'];
  46. $child_of = ( isset( $args[0]['child_of'] ) ) ? (int) $args[0]['child_of'] : 0;
  47.  
  48. /* Loop elements */
  49. foreach ( (array) $elements as $e ) {
  50. $parent_id = $e->$parent_field;
  51. if ( isset( $parent_id ) ) {
  52. /* Top level pages. */
  53. if( $child_of === $parent_id ) {
  54. $top_level_elements[] = $e;
  55. }
  56. /* Only display children of the current hierarchy. */
  57. else if (
  58. ( isset( $post->ID ) && $parent_id == $post->ID ) ||
  59. ( isset( $post->post_parent ) && $parent_id == $post->post_parent ) ||
  60. ( isset( $post->ancestors ) && in_array( $parent_id, (array) $post->ancestors ) )
  61. ) {
  62. $children_elements[ $e->$parent_field ][] = $e;
  63. }
  64. }
  65. }
  66.  
  67. /* Define output. */
  68. foreach ( $top_level_elements as $e ) {
  69. $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
  70. }
  71. return $output;
  72. }
  73. }
  74.  
  75.  
  76. Insert into template:
  77. /**
  78. * This will walk the navigation tree but exclude the top level pages.
  79. * handy for left-hand-nav links
  80. */
  81. <?php
  82. $root_page_id = ( empty( $post->ancestors ) ) ? $post->ID : end( $post->ancestors );
  83. $walker = new Razorback_Walker_Page_Selective_Children();
  84. wp_list_pages( array(
  85. 'title_li' => '',
  86. 'sort_column' => 'menu_order',
  87. 'child_of' => $root_page_id,
  88. 'walker' => $walker,
  89. ) );
  90. ?>

URL: http://wordpress.mfields.org/2010/selective-page-hierarchy-for-wp_list_pages/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.