1

I've made a custom walker to use with wp_list_pages to modify the output to have thumbnails show. The goal is to have a list show up in a page template, which lists all children with their children.

The walker I have below seems to work for getting the thumbnail and page title but doesn't get the url of the page. Do I need to somehow use a getprefixed function instead of just esc_attr?

            class bio_walker extends Walker_Nav_Menu
            {

            function start_el(&$output, $item, $depth, $args)
            {


            $output .= "<li id='menu-item-$item->ID' class='bio-list'>";

            $attributes  = "class='highlight-$item->ID'";

            ! empty( $item->attr_title )
                and $attributes .= ' title="'  . esc_attr( $item->attr_title ) .'"';
            ! empty( $item->target )
                and $attributes .= ' target="' . esc_attr( $item->target     ) .'"';
            ! empty( $item->xfn )
                and $attributes .= ' rel="'    . esc_attr( $item->xfn        ) .'"';
            ! empty( $item->url )
                and $attributes .= ' href="'   . esc_attr( $item->url        ) .'"';


            $pageid = get_post_meta ( $item->ID, '_menu_item_object_id', true);
            $thumbnail = get_the_post_thumbnail( $item->ID, 'bio-thumb', true);


            $title = get_the_title($item->ID);

            $spanclass = "class='span-$item->ID bio-list-span'";

            $item_output = $args->before
                . "<a $attributes>"
                . $thumbnail
                . "<span $spanclass>"
                . $title
                . $pageid
                . "</span>"
                . '</a> ';

            $output .= apply_filters(
                'walker_nav_menu_start_el'
            ,   $item_output
            ,   $item
            ,   $depth
            ,   $args
            );
            }
            }

2 Answers 2

1

figured it out - in case anyone else stumbles upon this with a similar question:

The proper walker to extend is Walker_Page, not Walker_Nav_Menu.

Walker_page is located in /wp-includes/post-template.php (line 978).

0

Try to link the menu to the location (on WP dashboard > appearance > Menus) which you're linking the walker to.

I.e if you have:

wp_nav_menu(array(
  'walker' => new custom_walker(),
  'theme_location' => 'primary',
))

Go ahead and link the menu you want to work with on WP Dashboard to the 'primary' location, so the walker can effectively access the $item object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.