I’m currently working on getting the documentation together for our API (with Fresh Vine) and I needed a bit of code to place in the sidebar that would display any resources from the same family.
Within WordPress I have the pages all nicely nested. So there is a top-level page with a slug ‘api’ (page ID 16), and that has a child slugged ‘documentation’ (page ID 20). This related pages script will check for the children of ‘documentation’ including the first level which we add in at the beginning there.
The page hierarchy looks like: api-> documentation->members->view-id (for example)
The Code
<!--?<span class="hiddenSpellError" pre=""-->php $Page = get_post(); // Get the Current Page $ParentID = $Page->post_parent; // Get the Parent Page ID if( $ParentID == 16 ) $ParentID = NULL; // We're a child of else if( $ParentID == 20 ) $ParentID = $Page->ID; // We $Related = get_pages( array( 'child_of' => $ParentID, 'sort_order' => 'ASC', 'sort_column' => 'post_title', 'hierarchical' => 0, 'parent' => -1, 'post_type' => 'page', 'post_status' => 'publish' ) ); // Look to see if there are any related pages. if( !is_null( $ParentID ) ){ // Get the Parent Page $Parent = get_page( $ParentID ); ?> <a href="<?php echo get_page_link( $ParentID ); ?>"> <!--?php echo $Parent--->post_title; ?>></a> <!--?php foreach( $Related as $rPage ) { if ( ! $rPage--->post_content ) // Check for empty page continue; ?> <a href="<?php echo get_page_link( $rPage->ID ); ?>"> <!--?php echo $rPage--->post_title; ?></a> <!--?php}// End foreach loop on Related pages } // End the related page check ?-->
Final Thoughts
All in all the code works splendidly. Though I have taken out all of my styling for this example I have it setup to display the method and to give visual indicators of what page the user is currently on (with a simple if $rPage->ID == $Page-ID check).
Well I hope this was helpful for someone else out there in the inter-webs. If you used the code leave a note so I can see how you used it!