Created
May 15, 2020 17:06
-
-
Save nickstewart95/143d7bca11ae78072f7604d5a3ea7d1f to your computer and use it in GitHub Desktop.
This file contains 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
// Inject partials into main menu | |
add_filter('wp_nav_menu_items', function ($items, $args) { | |
if ($args->menu->name != 'Primary') { | |
return $items; | |
} | |
$dom = new DOMDocument(); | |
$dom->loadHTML($items); | |
$xpath = new DOMXpath($dom); | |
$find = $xpath->query('//li[contains(@class, "product-menu")]|//li[contains(@class, "resource-menu")]'); | |
$products = $dom->createDocumentFragment(); | |
$products->appendXml(silencio_partial('partials/product-sub-menu', [], false)); | |
$resources = $dom->createDocumentFragment(); | |
$resources->appendXml(silencio_partial('partials/resources-sub-menu', [], false)); | |
foreach ($find as $item) { | |
$class = $item->getAttribute('class'); | |
if (stripos($class, 'product-menu') !== false) { | |
$item->appendChild($products); | |
} else if (stripos($class, 'resource-menu') !== false) { | |
$item->appendChild($resources); | |
} | |
} | |
return $dom->saveHTML(); | |
}, 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment