Skip to content

Instantly share code, notes, and snippets.

@idpokute
Created May 16, 2019 14:41
Show Gist options
  • Save idpokute/22a87ebec8b3819eef086002b9776181 to your computer and use it in GitHub Desktop.
Save idpokute/22a87ebec8b3819eef086002b9776181 to your computer and use it in GitHub Desktop.
Wordpress Walker 101
<?php
class CustomWalker extends Walker {
public $tree_type = 'category';
public $db_fields = array(
'parent' => 'parent',
'id' => 'term_id',
);
public function start_lvl( &$output, $depth = 0, $args = array() ) {
$output .= "<ul class='children'>\n";
}
public function end_lvl( &$output, $depth = 0, $args = array() ) {
$output .= "</ul>\n";
}
public function start_el( &$output, $item, $depth = 0, $args = array(), $current_object_id = 0 ) {
$output .= "<li>" . $item->name . "\n";
}
public function end_el( &$output, $category, $depth = 0, $args = array() ) {
$output .= "</li>\n";
}
}
$lists = array(
(object)[
'term_id' => '1',
'name' => 'A',
'parent' => 0,
],
(object)[
'term_id' => '2',
'name' => 'B',
'parent' => 0,
],
(object)[
'term_id' => '3',
'name' => 'C',
'parent' => 0,
],
(object)[
'term_id' => '4',
'name' => 'D',
'parent' => 1,
],
(object)[
'term_id' => '5',
'name' => 'E',
'parent' => 4,
],
);
$mywalker = new CustomWalker();
echo $mywalker->walk( $lists, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment