Skip to content

Instantly share code, notes, and snippets.

@tjsingleton
Created March 26, 2010 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjsingleton/345129 to your computer and use it in GitHub Desktop.
Save tjsingleton/345129 to your computer and use it in GitHub Desktop.
Refactoring a piece of code I came across that generates a menu. Saved each step so you can follow along. Opps, forgot to save the original code :)
<div id="accordion">
<?php
// just cleaning up formatting
define("MASTER_CAT_ID", 280);
define("CHILD_LIMIT", 200);
define("COMMON_GET_PAGE_ARGS", 'sort_column=menu_order&sort_order=asc&number='.CHILD_LIMIT);
function get_children_pages_of($cat_id) {
return get_pages(COMMON_GET_PAGE_ARGS.'&child_of='.$cat_id.'&parent='.$cat_id);
}
function each_page($pages, $function) {
foreach($pages as $page) { $function($page); }
}
function link_to_page($page) {
?>
<a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a>
<?php
}
function print_top_level_menu_item($page) {
?>
<div>
<h3><?php link_to_page($page); ?></h3>
<div>
<ul>
<?php each_page(get_children_pages_of($page->ID), 'print_secondary_menu_item'); ?>
</ul>
</div>
</div>
<?php
}
function print_secondary_menu_item($page) {
?>
<li><div><?php link_to_page($page); ?></div></li>
<?php
}
each_page(get_children_pages_of(MASTER_CAT_ID), 'print_top_level_menu_item');
?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment