Skip to content

Instantly share code, notes, and snippets.

@alphabet
Created August 13, 2018 14:59
Show Gist options
  • Save alphabet/de96986388ccc497b1e4aa285a25f72c to your computer and use it in GitHub Desktop.
Save alphabet/de96986388ccc497b1e4aa285a25f72c to your computer and use it in GitHub Desktop.
wordpress json custom walker example
<?php
class Walker_Json extends Walker {
// Tell Walker where to inherit it's parent and id values
var $db_fields = array(
'parent' => 'menu_item_parent',
'id' => 'db_id'
);
/**
* At the start of each element, output a <li> and <a> tag structure.
*
* Note: Menu objects include url and title properties, so we will use those.
*/
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$returnItem = array(
"item" => $item
);
echo json_encode($returnItem) . ",";
}
}
?>
<script>
<?php // BEGIN WORDPRESS NAVIGATION //?>
var jsonMenu = [<?php
wp_nav_menu(array(
'name' => 'Main',
'container' => false,
'walker' => new Walker_Json()
));
?>]
<?php // END WORDPRESS NAVIGATION //?>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment