我不确定这是一个答案,但更多的是一个讨论点。
有没有人考虑过构建WP菜单的高级自定义字段?我已经做过几次了,它允许我构建一个自定义结构以及自定义菜单项属性,并构建菜单的HTML,而无需使用默认WP菜单所需的复杂walker。
ACF
if( function_exists(\'acf_add_local_field_group\') ):
acf_add_local_field_group(array (
\'key\' => \'group_56532ec144a4b\',
\'title\' => \'Menu\',
\'fields\' => array (
array (
\'key\' => \'field_5653338918f43\',
\'label\' => \'Menus\',
\'name\' => \'menus\',
\'type\' => \'flexible_content\',
\'instructions\' => \'\',
\'required\' => 0,
\'conditional_logic\' => 0,
\'wrapper\' => array (
\'width\' => \'\',
\'class\' => \'\',
\'id\' => \'\',
),
\'button_label\' => \'Add Menu\',
\'min\' => \'\',
\'max\' => \'\',
\'layouts\' => array (
array (
\'key\' => \'56533396b10bc\',
\'name\' => \'menu\',
\'label\' => \'Menu\',
\'display\' => \'block\',
\'sub_fields\' => array (
array (
\'key\' => \'field_56533fc6f25e7\',
\'label\' => \'Menu Name\',
\'name\' => \'menu__name\',
\'type\' => \'text\',
\'instructions\' => \'\',
\'required\' => 0,
\'conditional_logic\' => 0,
\'wrapper\' => array (
\'width\' => \'\',
\'class\' => \'\',
\'id\' => \'\',
),
\'default_value\' => \'\',
\'placeholder\' => \'\',
\'prepend\' => \'\',
\'append\' => \'\',
\'maxlength\' => \'\',
\'readonly\' => 0,
\'disabled\' => 0,
),
array (
\'key\' => \'field_56532ec718f40\',
\'label\' => \'Menu Items\',
\'name\' => \'menu__items\',
\'type\' => \'flexible_content\',
\'instructions\' => \'\',
\'required\' => 0,
\'conditional_logic\' => 0,
\'wrapper\' => array (
\'width\' => \'\',
\'class\' => \'\',
\'id\' => \'\',
),
\'button_label\' => \'Add Menu Item\',
\'min\' => \'\',
\'max\' => \'\',
\'layouts\' => array (
array (
\'key\' => \'56532eee6ef81\',
\'name\' => \'menuItem\',
\'label\' => \'Menu Item\',
\'display\' => \'block\',
\'sub_fields\' => array (
array (
\'key\' => \'field_56532f0418f41\',
\'label\' => \'Label\',
\'name\' => \'menuITem__label\',
\'type\' => \'text\',
\'instructions\' => \'\',
\'required\' => 0,
\'conditional_logic\' => 0,
\'wrapper\' => array (
\'width\' => 50,
\'class\' => \'\',
\'id\' => \'\',
),
\'default_value\' => \'\',
\'placeholder\' => \'\',
\'prepend\' => \'\',
\'append\' => \'\',
\'maxlength\' => \'\',
\'readonly\' => 0,
\'disabled\' => 0,
),
array (
\'key\' => \'field_565333d218f45\',
\'label\' => \'Class\',
\'name\' => \'menuItem__class\',
\'type\' => \'text\',
\'instructions\' => \'\',
\'required\' => 0,
\'conditional_logic\' => 0,
\'wrapper\' => array (
\'width\' => 50,
\'class\' => \'\',
\'id\' => \'\',
),
\'default_value\' => \'\',
\'placeholder\' => \'\',
\'prepend\' => \'\',
\'append\' => \'\',
\'maxlength\' => \'\',
\'readonly\' => 0,
\'disabled\' => 0,
),
array (
\'key\' => \'field_565342ef11b29\',
\'label\' => \'Link Type\',
\'name\' => \'menuItem__type\',
\'type\' => \'radio\',
\'instructions\' => \'\',
\'required\' => 0,
\'conditional_logic\' => 0,
\'wrapper\' => array (
\'width\' => 25,
\'class\' => \'\',
\'id\' => \'\',
),
\'choices\' => array (
\'page\' => \'Page\',
\'cat\' => \'Category\',
\'url\' => \'URL\',
\'cust\' => \'Custom\',
),
\'other_choice\' => 0,
\'save_other_choice\' => 0,
\'default_value\' => \'\',
\'layout\' => \'vertical\',
),
array (
\'key\' => \'field_56532f2d18f42\',
\'label\' => \'Page\',
\'name\' => \'menuItem__page\',
\'type\' => \'page_link\',
\'instructions\' => \'\',
\'required\' => 0,
\'conditional_logic\' => array (
array (
array (
\'field\' => \'field_565342ef11b29\',
\'operator\' => \'==\',
\'value\' => \'page\',
),
),
),
\'wrapper\' => array (
\'width\' => 75,
\'class\' => \'\',
\'id\' => \'\',
),
\'post_type\' => array (
),
\'taxonomy\' => array (
),
\'allow_null\' => 0,
\'multiple\' => 0,
),
array (
\'key\' => \'field_5653434f11b2a\',
\'label\' => \'Category\',
\'name\' => \'menuItem__cat\',
\'type\' => \'taxonomy\',
\'instructions\' => \'\',
\'required\' => 0,
\'conditional_logic\' => array (
array (
array (
\'field\' => \'field_565342ef11b29\',
\'operator\' => \'==\',
\'value\' => \'cat\',
),
),
),
\'wrapper\' => array (
\'width\' => 75,
\'class\' => \'\',
\'id\' => \'\',
),
\'taxonomy\' => \'category\',
\'field_type\' => \'select\',
\'allow_null\' => 0,
\'add_term\' => 1,
\'save_terms\' => 0,
\'load_terms\' => 0,
\'return_format\' => \'id\',
\'multiple\' => 0,
),
array (
\'key\' => \'field_5653439311b2c\',
\'label\' => \'Custom\',
\'name\' => \'menuItem__cstm\',
\'type\' => \'text\',
\'instructions\' => \'\',
\'required\' => 0,
\'conditional_logic\' => array (
array (
array (
\'field\' => \'field_565342ef11b29\',
\'operator\' => \'==\',
\'value\' => \'cust\',
),
),
),
\'wrapper\' => array (
\'width\' => 75,
\'class\' => \'\',
\'id\' => \'\',
),
\'default_value\' => \'\',
\'placeholder\' => \'\',
\'prepend\' => \'\',
\'append\' => \'\',
\'maxlength\' => \'\',
\'readonly\' => 0,
\'disabled\' => 0,
),
array (
\'key\' => \'field_5653437011b2b\',
\'label\' => \'URL\',
\'name\' => \'menuItem__url\',
\'type\' => \'url\',
\'instructions\' => \'\',
\'required\' => 0,
\'conditional_logic\' => array (
array (
array (
\'field\' => \'field_565342ef11b29\',
\'operator\' => \'==\',
\'value\' => \'url\',
),
),
),
\'wrapper\' => array (
\'width\' => 75,
\'class\' => \'\',
\'id\' => \'\',
),
\'default_value\' => \'\',
\'placeholder\' => \'\',
),
),
\'min\' => \'\',
\'max\' => \'\',
),
),
),
),
\'min\' => \'\',
\'max\' => \'\',
),
),
),
),
\'location\' => array (
array (
array (
\'param\' => \'options_page\',
\'operator\' => \'==\',
\'value\' => \'acf-options-theme-options\',
),
),
),
\'menu_order\' => 0,
\'position\' => \'normal\',
\'style\' => \'default\',
\'label_placement\' => \'top\',
\'instruction_placement\' => \'label\',
\'hide_on_screen\' => \'\',
\'active\' => 1,
\'description\' => \'\',
));
endif;
UX
function acfMenu($name) {
if( function_exists(\'get_field\') ) :
$getMenus = get_field(\'menus\', \'option\');
foreach($getMenus as $menuData) :
if( $menuData[\'menu__name\'] == $name ) :
// Do stuff to build your menu
endif;
endforeach;
endif;
}
这只是一个简单的示例,但使用ACF提供的选项,您可以将各种内容添加到菜单项上,然后随意编写UI。
在应用于您的特定问题时,可以依次将条件选择应用于您提供的链接类型(请参见附图)。ACF允许许多不同的参数,如页面链接、类别链接或直接URL。有条件地选择这些项将允许更改菜单项的类型,而无需删除类或其他属性。