Skip to content

Instantly share code, notes, and snippets.

@Jehu
Last active Feb 16, 2022
Embed
What would you like to do?
WordPress: Menu for Child pages (Split Menu)
<?php
function mwe_get_parent_title() {
$post = get_post();
return get_the_title($post->post_parent);
}
function mwe_get_sidebar_menu() {
$post = get_post();
if ( is_page($post->ID) && $post->post_parent ) {
$children = get_pages(array(
'child_of' => $post->post_parent,
'sort_column' => 'menu_order',
)
);
} else {
$children = get_pages(array(
'child_of' => $post->ID,
'sort_column' => 'menu_order',
)
);
}
?>
<ul class="submenu">
<?php
foreach ($children as $child) {
$is_active = ($child->ID === get_the_ID()) ? 'active' : '';
?>
<li class="submenu__item"><a class="submenu__link <?= $is_active ?>" href="<?= get_permalink($child->ID) ?>">
<?= $child->post_title ?>
</a></li>
<?php } ?>
</ul>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment