94 lines
3.2 KiB
PHP
94 lines
3.2 KiB
PHP
<?php
|
|
|
|
/* -----------------------------------------------------------
|
|
Walker to ReWrap li submenu parent with button instead of
|
|
-----------------------------------------------------------*/
|
|
|
|
function wrap_parent_menu_item_buttons($output, $item, $depth, $args)
|
|
{
|
|
// #### MENU HOMEGRADE HEADER
|
|
if ($args->theme_location === "secondary" && in_array('menu-item-has-children', $item->classes, true)) {
|
|
|
|
$output = '<button type="button" class="menu-item__submenu-toggle" aria-expanded="false" aria-controls="sub-menu-' . $item->ID . '">' . $item->title . '</button>';
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
add_filter('walker_nav_menu_start_el', 'wrap_parent_menu_item_buttons', 10, 4);
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
##### Adds option 'li_class' to 'wp_nav_menu
|
|
------------------------------------------------------------------*/
|
|
|
|
function tailpress_nav_menu_add_li_class($classes, $item, $args, $depth)
|
|
{
|
|
if (isset($args->li_class)) {
|
|
$classes[] = $args->li_class;
|
|
}
|
|
|
|
if (isset($args->{"li_class_$depth"})) {
|
|
$classes[] = $args->{"li_class_$depth"};
|
|
}
|
|
|
|
return $classes;
|
|
}
|
|
|
|
add_filter('nav_menu_css_class', 'tailpress_nav_menu_add_li_class', 10, 4);
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
##### Adds option 'submenu_class' to 'wp_nav_menu'
|
|
------------------------------------------------------------------*/
|
|
|
|
function tailpress_nav_menu_add_submenu_class($classes, $args, $depth)
|
|
{
|
|
if (isset($args->submenu_class)) {
|
|
$classes[] = $args->submenu_class;
|
|
}
|
|
|
|
if (isset($args->{"submenu_class_$depth"})) {
|
|
$classes[] = $args->{"submenu_class_$depth"};
|
|
}
|
|
|
|
return $classes;
|
|
}
|
|
|
|
add_filter('nav_menu_submenu_css_class', 'tailpress_nav_menu_add_submenu_class', 10, 3);
|
|
|
|
/* ----------------------------------------------------------------
|
|
##### Inject ACF page_icon into submenu items
|
|
------------------------------------------------------------------*/
|
|
function carhop_add_submenu_item_icon($output, $item, $depth, $args)
|
|
{
|
|
// Only for submenu items (depth >= 1
|
|
if (!isset($args->theme_location) || $args->theme_location !== 'secondary' || $depth < 1 || !function_exists('get_field')) {
|
|
return $output;
|
|
}
|
|
|
|
// Try fetching the icon from the menu item itself, then fallback to linked object (e.g., page)
|
|
$pageIcon = get_field('page_icon', $item->object_id);
|
|
|
|
if (empty($pageIcon)) {
|
|
return $output;
|
|
}
|
|
|
|
|
|
$icon_html = '';
|
|
$excerpt_html = '';
|
|
// Attachment array with ID (ACF image field)
|
|
if (is_array($pageIcon) && isset($pageIcon['url'])) {
|
|
$icon_html = '<img src="' . esc_url($pageIcon['url']) . '" class="menu-item__icon-img" alt="" aria-hidden="true" />';
|
|
}
|
|
|
|
if (has_excerpt($item->object_id)) {
|
|
$excerpt_html = '<p class="menu-item__excerpt">' . get_the_excerpt($item->object_id) . '</p>';
|
|
}
|
|
|
|
|
|
return '<a class="menu-item__content" href="' . $item->url . '">' . $icon_html . '<div class="menu-item__content-inner"><p class="menu-item__title">' . $item->title . '</p>' . $excerpt_html . '</div></a>';
|
|
}
|
|
add_filter('walker_nav_menu_start_el', 'carhop_add_submenu_item_icon', 15, 4);
|