-
-
Save pavlo-bondarchuk/8b927c9b723b58eb22df71997f783a1d to your computer and use it in GitHub Desktop.
get_wp_menu_by_chunk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get_wp_menu_by_chunk($menu_location, $max_items_per_column = 5) | |
{ | |
$menu_locations = get_nav_menu_locations(); | |
if (!isset($menu_locations[$menu_location])) { | |
return; | |
} | |
$menu = wp_get_nav_menu_object($menu_locations[$menu_location]); | |
if (!$menu) { | |
return; | |
} | |
$menu_items = wp_get_nav_menu_items($menu->term_id); | |
if (!$menu_items) { | |
return; | |
} | |
$chunks = array_chunk($menu_items, $max_items_per_column); | |
$total_items = count($menu_items); | |
$item_count = 0; | |
foreach ($chunks as $chunk) { | |
$item_count += count($chunk); | |
$ul_class = ($item_count <= 10) ? ' class="d-none d-xl-block"' : ''; | |
echo '<ul' . $ul_class . '>'; | |
foreach ($chunk as $item) { | |
echo '<li><a href="' . esc_url($item->url) . '">' . esc_html($item->title) . '</a></li>'; | |
} | |
echo '</ul>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment