Skip to content

Instantly share code, notes, and snippets.

@mattboon
Created September 1, 2012 15:30
Show Gist options
  • Save mattboon/3577343 to your computer and use it in GitHub Desktop.
Save mattboon/3577343 to your computer and use it in GitHub Desktop.
WordPress - Create in-section navigation for a given (hierarchical) post type
<?php
// Return link by post ID
// -------------------------------------------------------------
function link_by_id($post_id) {
echo '<a href="'.get_permalink($post_id).'">'.get_the_title($post_id).'</a>';
}
// Return the root parent ID
// -------------------------------------------------------------
function get_root_parent_id($post_id = null) {
if(!$post_id) {
global $post;
$post_id = $post->ID;
}
global $wpdb;
$parent = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE post_type='page' AND post_status='publish' AND ID = '$page_id'");
if ($parent == 0) return $post_id;
else return root_parent_id($parent);
}
// Return root posts of any given post type
// -------------------------------------------------------------
function get_root_pages($post_type = 'page') {
$args = array(
'post_type' => $post_type,
'parent' => 0,
'order' => 'ASC',
'orderby' => 'menu_order',
'numberposts' => -1
);
return get_pages($args);
}
// Return sub pages of a post
// -------------------------------------------------------------
function get_sub_pages($post_id, $post_type = 'page') {
$args = array(
'post_type' => $post_type,
'child_of' => $post_id,
'parent' => $post_id,
'sort_order' => 'ASC',
'sort_column' => 'menu_order'
);
return get_pages($args);
}
// PLUGIN - Generate uber sub-navigation (in this section)
// -------------------------------------------------------------
// boons_insection_subnav('product', 'current');
// Usage: 'product' = post type and 'current' = current class for <li>
// -------------------------------------------------------------
function boons_insection_subnav($post_type = 'page', $current_class = 'current') {
// Test if current page in sub navigation
// -------------------------------------------------------------
function subnav_is_current($post_id) {
global $post;
return ($post_id==$post->ID);
}
// Test next level in sub navigation
// -------------------------------------------------------------
function subnav_next_level($post_id, $next_level) {
global $post;
return ($next_level && subnav_is_current($post_id) || in_array($post_id, $post->ancestors));
}
// Output subnav
// -------------------------------------------------------------
// level 1
$sub_level_1 = get_root_pages($post_type);
if($sub_level_1): ?>
<ul class="navigation-secondary level-1">
<?php
// level 1 items
foreach($sub_level_1 as $level_1_page):
$level_1_page_id = $level_1_page->ID;
?>
<li class="level-1-item<?= subnav_is_current($level_1_page_id) ? ' '.$current_class.'' : ''; ?>">
<?php link_by_id($level_1_page_id); ?>
<?php
// level 2
$sub_level_2 = get_sub_pages($level_1_page_id, $post_type);
if(subnav_next_level($level_1_page_id, $sub_level_2)): ?>
<ul class="level-2">
<?php
// level 2 items
foreach($sub_level_2 as $level_2_page):
$level_2_page_id = $level_2_page->ID;
?>
<li class="level-2-item<?= subnav_is_current($level_2_page_id) ? ' '.$current_class.'' : ''; ?>">
<?php link_by_id($level_2_page_id); ?>
<?php
// level 3
$sub_level_3 = get_sub_pages($level_2_page_id, $post_type);
if(subnav_next_level($level_2_page_id, $sub_level_3)): ?>
<ul class="level-3">
<?php
// level 3 items
foreach($sub_level_3 as $level_3_page):
$level_3_page_id = $level_3_page->ID;
?>
<li class="level-3-item<?= subnav_is_current($level_3_page_id) ? ' '.$current_class.'' : ''; ?>">
<?php link_by_id($level_3_page_id); ?>
</li>
<?php
// level 3 items
endforeach; ?>
</ul>
<?php
// level 3
endif; ?>
</li>
<?php
// level 2 items
endforeach; ?>
</ul>
<?php
// level 2
endif; ?>
</li>
<?php
// level 1 items
endforeach; ?>
</ul>
<?php else: ?>
<em>No <?= $post_type; ?>s added yet</em>
<?php endif; ?>
<?
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment