$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>customization - Implement a Walker with custom object rather than Wordpress database object|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

customization - Implement a Walker with custom object rather than Wordpress database object

matteradmin8PV0评论

I am trying to implement a Custom Walker to navigate through a hierarchy of objects. When I do a var_dump on the $item variable in the start_el function I see a wordpress database object (blog, page, etc.) and not my custom Data Object.

How do I set up the Walker to accept my custom object for Menu hierarchy and navigation?

Thanks, Dave

I am trying to implement a Custom Walker to navigate through a hierarchy of objects. When I do a var_dump on the $item variable in the start_el function I see a wordpress database object (blog, page, etc.) and not my custom Data Object.

How do I set up the Walker to accept my custom object for Menu hierarchy and navigation?

Thanks, Dave

Share Improve this question edited Jan 30, 2019 at 7:33 Pratik Patel 1,1091 gold badge11 silver badges23 bronze badges asked Jan 30, 2019 at 0:42 DaveDave 11 bronze badge 0
Add a comment  | 

1 Answer 1

Reset to default 1

May not be exactly what you are after, but you could take a look at the Bootstrap Nav Walker implementation.

In the documentation, there's also an example for manually calling your own walker class which may be worth a read if you haven't done so already.

class Walker_Quickstart_Menu 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 ) {
        $output .= sprintf( "\n<li><a href='%s'%s>%s</a></li>\n",
            $item->url,
            ( $item->object_id === get_the_ID() ) ? ' class="current"' : '',
            $item->title
        );
    }
}

And then call the walker:

// 1. Fetch the menu (we'll assume it has an id of 2)...
$menu = wp_get_nav_menu_object(2);

// 2. Create an empty $menu_items array
$menu_items = array();

// 3. Get menu objects (this is our tree structure)
if ( $menu && ! is_wp_error($menu) && empty($menu_items) ) {
    $menu_items = wp_get_nav_menu_items( $menu );
}

// 4. Create a new instance of our walker...
$walk = new Walker_Quickstart_Menu();

// 5. Walk the tree and render the returned output as a one-dimensional array
print_r( $walk->walk( $menu_items, -1 ) );
Post a comment

comment list (0)

  1. No comments so far