Viewing 3 replies - 1 through 3 (of 3 total)
  • $items = array();
    
    foreach ( wp_get_nav_menu_items( 2 ) as $item ) {
    	$item->icon = array_filter( Menu_Icons_Meta::get( $item->ID ) );
    	$items[] = $item;
    }

    Example result:

    Array
    (
        [0] => WP_Post Object
            (
                [ID] => 1363
                [post_author] => 1
                [post_date] => 2016-05-26 16:08:05
                [post_date_gmt] => 2016-05-26 16:08:05
                [post_content] =>
                [post_title] => Home
                [post_excerpt] =>
                [post_status] => publish
                [comment_status] => closed
                [ping_status] => closed
                [post_password] =>
                [post_name] => home-2
                [to_ping] =>
                [pinged] =>
                [post_modified] => 2016-05-29 12:20:36
                [post_modified_gmt] => 2016-05-29 12:20:36
                [post_content_filtered] =>
                [post_parent] => 0
                [guid] => https://src.wordpress-develop.dev/site02/?p=1363
                [menu_order] => 1
                [post_type] => nav_menu_item
                [post_mime_type] =>
                [comment_count] => 0
                [filter] => raw
                [db_id] => 1363
                [menu_item_parent] => 0
                [object_id] => 1363
                [object] => custom
                [type] => custom
                [type_label] => Custom Link
                [title] => Home
                [url] => https://src.wordpress-develop.dev/site02/
                [target] =>
                [attr_title] =>
                [description] =>
                [classes] => Array
                    (
                        [0] =>
                    )
    
                [xfn] =>
                [icon] => Array
                    (
                        [type] => dashicons
                        [icon] => dashicons-admin-home
                        [position] => before
                        [vertical_align] => middle
                        [font_size] => 1.2
                        [svg_width] => 1
                        [image_size] => thumbnail
                    )
    
            )
    
        [1] => WP_Post Object
            (
                [ID] => 1364
                [post_author] => 1
                [post_date] => 2016-05-26 16:08:05
                [post_date_gmt] => 2016-05-26 16:08:05
                [post_content] =>
                [post_title] =>
                [post_excerpt] =>
                [post_status] => publish
                [comment_status] => closed
                [ping_status] => closed
                [post_password] =>
                [post_name] => 1364
                [to_ping] =>
                [pinged] =>
                [post_modified] => 2016-05-29 12:20:36
                [post_modified_gmt] => 2016-05-29 12:20:36
                [post_content_filtered] =>
                [post_parent] => 0
                [guid] => https://src.wordpress-develop.dev/site02/?p=1364
                [menu_order] => 2
                [post_type] => nav_menu_item
                [post_mime_type] =>
                [comment_count] => 0
                [filter] => raw
                [db_id] => 1364
                [menu_item_parent] => 0
                [object_id] => 1086
                [object] => page
                [type] => post_type
                [type_label] => Page
                [url] => https://src.wordpress-develop.dev/site02/about
                [title] => About
                [target] =>
                [attr_title] =>
                [description] =>
                [classes] => Array
                    (
                        [0] =>
                    )
    
                [xfn] =>
                [icon] => Array
                    (
                        [type] => image
                        [icon] => 963
                        [url] => https://src.wordpress-develop.dev/site02/wp-content/uploads/sites/2/2012/12/triforce-wallpaper-150x150.jpg
                        [position] => before
                        [vertical_align] => middle
                        [font_size] => 1.2
                        [svg_width] => 1
                        [image_size] => small-icon
                    )
    
            )
    
        [2] => WP_Post Object
            (
                [ID] => 1365
                [post_author] => 1
                [post_date] => 2016-05-26 16:08:05
                [post_date_gmt] => 2016-05-26 16:08:05
                [post_content] =>
                [post_title] =>
                [post_excerpt] =>
                [post_status] => publish
                [comment_status] => closed
                [ping_status] => closed
                [post_password] =>
                [post_name] => 1365
                [to_ping] =>
                [pinged] =>
                [post_modified] => 2016-05-29 12:20:36
                [post_modified_gmt] => 2016-05-29 12:20:36
                [post_content_filtered] =>
                [post_parent] => 0
                [guid] => https://src.wordpress-develop.dev/site02/?p=1365
                [menu_order] => 3
                [post_type] => nav_menu_item
                [post_mime_type] =>
                [comment_count] => 0
                [filter] => raw
                [db_id] => 1365
                [menu_item_parent] => 0
                [object_id] => 1062
                [object] => page
                [type] => post_type
                [type_label] => Page
                [url] => https://src.wordpress-develop.dev/site02/amazon-store
                [title] => Amazon Store
                [target] =>
                [attr_title] =>
                [description] =>
                [classes] => Array
                    (
                        [0] =>
                    )
    
                [xfn] =>
                [icon] => Array
                    (
                    )
    
            )
    );

    Notice the icon key added to each item. For image icon in the last item on the above example, the attachment ID is 963 and the selected size is small-icon which you can pass to wp_get_attachment_image* functions to get the actual URL.

    Example code:

    $items = array();
    
    foreach ( wp_get_nav_menu_items( 2 ) as $item ) {
    	$item->icon = array_filter( Menu_Icons_Meta::get( $item->ID ) );
    	$items[] = $item;
    }

    Thread Starter moeezalikhan

    (@moeezalikhan)

    Thank You very Much . It works

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘how to get Image icon url’ is closed to new replies.