Отображение прямого подменю родительского уровня в Wordpress - PullRequest
0 голосов
/ 10 апреля 2019

Я хочу создать подменю в моем шаблоне Wordpress, в котором будут отображаться только другие дочерние элементы этого родителя. Я нашел решение , которое помогает мне на 95% пути, и я не могу понять, как его получить до конца.Любая помощь будет высоко ценится!

Решение в коде ниже покажет это:

Раздел

  • Подраздел 1
  • Подраздел 2
    • Страница подраздела
    • Страница подраздела
  • Подраздел 3

Я хотел бы просто показатьподменю для прямого родителя, например:

Раздел

  • Подраздел 2
    • Страница подраздела
    • Страница подраздела
<div class="sidebar-nav">

<h3><?php
    if ( 0 == $post->post_parent ) {
    the_title();
    } else {
    $parents = get_post_ancestors( $post->ID );
    echo apply_filters( "the_title", get_the_title( end ( $parents ) ) );
    }
?></h3>

<?php
function hierarchical_submenu($post) {
    $top_post = $post;
    // If the post has ancestors, get its ultimate parent and make that the top post
    if ($post->post_parent && $post->ancestors) {
        $top_post = get_post(end($post->ancestors));
    }
    // Always start traversing from the top of the tree
    return hierarchical_submenu_get_children($top_post, $post);
}

function hierarchical_submenu_get_children($post, $current_page) {
    $menu = '';
    // Get all immediate children of this page
    $children = get_pages('child_of=' . $post->ID . '&parent=' . $post->ID . '&sort_column=menu_order&sort_order=ASC');
    if ($children) {
        $menu = "\n<ul>\n";
        foreach ($children as $child) {
            // If the child is the viewed page or one of its ancestors, highlight it
            if (in_array($child->ID, get_post_ancestors($current_page)) || ($child->ID == $current_page->ID)) {
                $menu .= '<li class="sel"><a href="' . get_permalink($child) . '" class="sel"><strong>' . $child->post_title . '</strong></a>';
            } else {
                $menu .= '<li><a href="' . get_permalink($child) . '">' . $child->post_title . '</a>';
            }
            // If the page has children and is the viewed page or one of its ancestors, get its children
            if (get_children($child->ID) && (in_array($child->ID, get_post_ancestors($current_page)) || ($child->ID == $current_page->ID))) {
                $menu .= hierarchical_submenu_get_children($child, $current_page);
            }
            $menu .= "</li>\n";
        }
        $menu .= "</ul>\n";
    }
    return $menu;
}
?>

<?php
$submenu = hierarchical_submenu($post);
if ($submenu) {
    echo $submenu;
} else {
    // Do something else
}
?>

</div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...