Created
August 13, 2018 14:59
-
-
Save alphabet/de96986388ccc497b1e4aa285a25f72c to your computer and use it in GitHub Desktop.
wordpress json custom walker example
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 | |
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