wp_nav_menu_objects循环仅对首个菜单项生效,ACF图标无法批量添加
问题
我为WordPress菜单项创建了ACF字段,参考ACF官方文档编写代码,尝试遍历菜单项输出图标,但图标仅附加到循环中的第一个菜单项。以下是我的代码:
functions.php 代码
add_filter('wp_nav_menu_objects', 'icon_wp_nav_menu_objects', 10, 2); function icon_wp_nav_menu_objects($items, $args) { if ($args->theme_location == 'main-menu') { // loop foreach ($items as &$item) { // vars $icon = get_field('icon', $item); // append icon if ($icon) { $item->title .= ' <img src="' . $icon . '" width="40px">'; } // return return $items; } } }
执行print_r($items)得到如下结果:
Array ( [1] => WP_Post Object( [ID] => 297[post_author] => 1[post_date] => 2023 - 08 - 21 13: 30: 30[post_date_gmt] => 2023 - 07 - 23 17: 19: 42[post_content] => [post_title] => [post_excerpt] => [post_status] => publish[comment_status] => closed[ping_status] => closed[post_password] => [post_name] => 297[to_ping] => [pinged] => [post_modified] => 2023 - 08 - 21 13: 30: 30[post_modified_gmt] => 2023 - 08 - 21 13: 30: 30[post_content_filtered] => [post_parent] => 0[guid] => http: //website.local/?p=297 [menu_order] => 1[post_type] => nav_menu_item[post_mime_type] => [comment_count] => 0[filter] => raw[db_id] => 297[menu_item_parent] => 0[object_id] => 208[object] => page[type] => post_type[type_label] => Page[url] => http: //website.local/menu-item/ [title] => Menu Item [target] => [attr_title] => [description] => [classes] => Array( [0] => [1] => menu - item[2] => menu - item - type - post_type[3] => menu - item - object - page[4] => menu - item - has - children ) [xfn] => [current] => [current_item_ancestor] => [current_item_parent] => ) [2] => WP_Post Object( [ID] => 298[post_author] => 1[post_date] => 2023 - 08 - 21 13: 30: 30[post_date_gmt] => 2023 - 07 - 23 17: 19: 42[post_content] => [post_title] => [post_excerpt] => [post_status] => publish[comment_status] => closed[ping_status] => closed[post_password] => [post_name] => 298[to_ping] => [pinged] => [post_modified] => 2023 - 08 - 21 13: 30: 30[post_modified_gmt] => 2023 - 08 - 21 13: 30: 30[post_content_filtered] => [post_parent] => 0[guid] => http: //website.local/?p=298 [menu_order] => 2[post_type] => nav_menu_item[post_mime_type] => [comment_count] => 0[filter] => raw[db_id] => 298[menu_item_parent] => 297[object_id] => 208[object] => page[type] => post_type[type_label] => Page[url] => http: //website.local/blog/ [title] => Blog [target] => [attr_title] => [description] => [classes] => Array( [0] => [1] => menu - item[2] => menu - item - type - post_type[3] => menu - item - object - page ) [xfn] => [current] => [current_item_ancestor] => [current_item_parent] => ) [3] => WP_Post Object( [ID] => 296[post_author] => 1[post_date] => 2023 - 08 - 21 13: 30: 30[post_date_gmt] => 2023 - 07 - 23 17: 19: 42[post_content] => [post_title] => [post_excerpt] => [post_status] => publish[comment_status] => closed[ping_status] => closed[post_password] => [post_name] => 296[to_ping] => [pinged] => [post_modified] => 2023 - 08 - 21 13: 30: 30[post_modified_gmt] => 2023 - 08 - 21 13: 30: 30[post_content_filtered] => [post_parent] => 0[guid] => http: //website.local/?p=296 [menu_order] => 3[post_type] => nav_menu_item[post_mime_type] => [comment_count] => 0[filter] => raw[db_id] => 296[menu_item_parent] => 297[object_id] => 289[object] => page[type] => post_type[type_label] => Page[url] => http: //website.local/page/ [title] => Page[target] => [attr_title] => [description] => [classes] => Array( [0] => [1] => menu - item[2] => menu - item - type - post_type[3] => menu - item - object - page ) [xfn] => [current] => [current_item_ancestor] => [current_item_parent] => ) [4] => WP_Post Object( [ID] => 55[post_author] => 1[post_date] => 2023 - 08 - 21 13: 30: 30[post_date_gmt] => 2023 - 07 - 15 13: 46: 30[post_content] => [post_title] => How can we help ? [post_excerpt] => [post_status] => publish[comment_status] => closed[ping_status] => closed[post_password] => [post_name] => link - 1[to_ping] => [pinged] => [post_modified] => 2023 - 08 - 21 13 : 30: 30[post_modified_gmt] => 2023 - 08 - 21 13: 30: 30[post_content_filtered] => [post_parent] => 0[guid] => http: //website.local/link-1/ [menu_order] => 4[post_type] => nav_menu_item[post_mime_type] => [comment_count] => 0[filter] => raw[db_id] => 55[menu_item_parent] => 0[object_id] => 55[object] => custom[type] => custom[type_label] => Custom Link[title] => Link [url] => https : //website.local/link/ [target] => [attr_title] => [description] => [classes] => Array( [0] => [1] => menu - item[2] => menu - item - type - custom[3] => menu - item - object - custom[4] => menu - item - has - children ) [xfn] => [current] => [current_item_ancestor] => [current_item_parent] => ) )
请问我哪里出错了?
解决方法
问题出在**return $items语句的位置错误**——它被放在了foreach循环内部。
当代码执行第一次循环时,处理完第一个菜单项后就直接返回了修改后的$items数组,循环根本没有机会处理剩下的菜单项,所以只有第一个菜单项会被添加图标。
修正后的代码需要把return $items移到循环和if判断的外面,确保所有菜单项都被遍历处理完后再返回结果:
add_filter('wp_nav_menu_objects', 'icon_wp_nav_menu_objects', 10, 2); function icon_wp_nav_menu_objects($items, $args) { if ($args->theme_location == 'main-menu') { // 遍历所有菜单项 foreach ($items as &$item) { // 获取ACF图标字段 $icon = get_field('icon', $item); // 如果图标存在,追加到标题后 if ($icon) { $item->title .= ' <img src="' . esc_url($icon) . '" width="40px">'; } // 循环内部不再保留return语句 } // 所有菜单项处理完成后再返回 return $items; } // 如果不是目标菜单,直接返回原数组 return $items; }
另外建议加上esc_url()函数对图标URL进行转义处理,避免潜在的安全问题和URL格式错误。
内容的提问来源于stack exchange,提问作者emilyyyyyy
相关产品推荐
相关产品推荐

