Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm attempting to build a custom navigation walker that pulls a page's Featured Image into the page's menu item. I intend to use the Featured Image as a background for the menu item (which is prettier than it sounds). I can get the image itself to be pulled in, but I can't seem to isolate its URL, which I require for the generated inline CSS.

I feel as if I'm not understanding the proper way to pull attachment IDs or elements out of their arrays. I've been hitting my head for hours against this problem, even when trying out solutions from similar discussions on Wordpress Answers. But no dice.

Here's the code that successfully grabs the thumbnail, but returns a blank when I try to get its URL directly.

$thumbnail_url = '';
if ( has_post_thumbnail( $item->object_id ) ) {
                $thumbnail_img = get_the_post_thumbnail ( $item->object_id );
                $thumbnail_array = wp_get_attachment_image_src( $thumbnail_img );
                $thumbnail_url = $thumbnail_array[0];
        }

For reference, here's the construction of code in the start_el function before it:

function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

        $class_names = $value = '';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'menu-item-' . $item->ID;

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

        $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

        $output .= $indent . '';

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

Thanks for anyone's help in advance.

share|improve this question
    
I figured it out: The code that works to pull the URL only is: ` if ( has_post_thumbnail( $item->object_id ) ) { $thumbnail_array = wp_get_attachment_image_src( get_post_thumbnail_id( $item->object_id ), 'large', false ); $thumbnail_url = $thumbnail_array[0]; } ` -- then I just output the $thumbnail_url variable in my output code for each menu item. –  Rob Butz Dec 18 '13 at 6:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.