Last active
August 29, 2015 14:01
-
-
Save jb510/67f75dc532c70e790158 to your computer and use it in GitHub Desktop.
Single Page Navigation of grandchildren skipping parents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function lm_do_video_navigation() { | |
// This runs on the grandchild. It finds the grandparent, then it's children, and loops through | |
// the children in order building an array of the grandchildren in menu_order. | |
// get top parent post ID | |
$top_parent_ID = get_page(array_pop(get_post_ancestors($post->ID)))->ID; | |
$args = array( | |
'post_type' => 'course', | |
'child_of' => $top_parent_ID, | |
'parent' => $top_parent_ID, | |
'hierarchical' => 0, | |
'sort_column' => 'menu_order', | |
'sort_order' => 'asc' | |
); | |
$sections = get_pages( $args ); | |
// Initialize array used inside nested | |
$video_IDs = array(); | |
// We need to build the video id array by sections because menu_order may not span sections | |
// otherwise we could use post_parent__in array and use one query :( | |
foreach ($sections as $section) { | |
$args = array( | |
'post_type' => 'course', | |
'parent' => $section->ID, | |
'number' => 50, // Return all, but limit for sanity check | |
'sort_column' => 'menu_order', | |
'sort_order' => 'ASC', | |
'fields' => 'ids', | |
); | |
$videos = get_pages($args); | |
// Turn post objects into an array of IDs | |
foreach ($videos as $video) { | |
array_push($video_IDs, $video->ID); | |
} | |
} | |
$current = array_search(get_the_ID(), $video_IDs); | |
$prevID = $video_IDs[$current-1]; | |
$nextID = $video_IDs[$current+1]; | |
echo '<div class="pagination">'; | |
if (!empty($prevID)) { | |
echo '<div class="alignleft">'; | |
echo'<a href="' . get_permalink($prevID) . '" title="' . get_the_title($prevID) . '">Previous Video<br/><span class="pagination-title">' . get_the_title($prevID) . '</span></a>'; | |
echo '</div>'; | |
} | |
if (!empty($nextID)) { | |
echo '<div class="alignright">'; | |
echo'<a href="' . get_permalink($nextID) . '" title="' . get_the_title($nextID) . '">Next Video<br/><span class="pagination-title">' . get_the_title($nextID) . '</span></a>'; | |
echo '</div>'; | |
} | |
echo '</div><!-- .pagination -->'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment