Add Submenu to Wordpress Theme

I want to add a Wordpress menu submenu to my theme. I want to use the wp_nav_menu function of Wordpress 3.0. In other words, I want to see a submenu, not a subpage, which means that wp_list_pages is not the right function, because I want a submenu, not a subpage.

Suppose the menu structure looks like this:

  • home
  • entry1
    • Entry3
    • Entry4
  • entry2
    • Entry5
    • Entry6

I want that if someone clicks on Entry1 (and makes him a parent), the topic simply shows a submenu of this entry. In the case of Entry1, this is:

  • Entry3
  • Entry4

I know there is a code like this:

<?php 
    $children = ($post->post_parent) ? wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0') : wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0'); 
    if($children) { echo('<ul>'.$children.'</ul>'); } 
 ?> 

However, the fact is that I'm talking about the structure of the menu, and not about the structure of the page. Oh, and the depth parameter doesn't work, because it means here, not from here.

I think there may be a solution with a custom walker, but I don't know how to implement this.

wp_nav_menu http://codex.wordpress.org/Template_Tags/wp_nav_menu

, , , . .

+3
3

, .sub-, . , ".current_page_item.sub-menu"

$(document).ready(function() {
        $(".sub-menu").hide(); // hide the submenu on page load
    $(".current_page_item .sub-menu").show();
)};
0

: http://www.svennerberg.com/2009/02/creating-a-submenu-in-wordpress/

<?php
$has_subpages = false;
// Check to see if the current page has any subpages
$children = wp_list_pages('&child_of='.$post->ID.'&echo=0');
if($children) {
    $has_subpages = true;
}
// Reseting $children
$children = "";

// Fetching the right thing depending on if we're on a subpage or on a parent page (that has subpages)
if(is_page() && $post->post_parent) {
    // This is a subpage
    $children = wp_list_pages("title_li=&include=".$post->post_parent ."&echo=0");
    $children .= wp_list_pages("title_li=&child_of=".$post->post_parent ."&echo=0");
} else if($has_subpages) {
    // This is a parent page that have subpages
    $children = wp_list_pages("title_li=&include=".$post->ID ."&echo=0");
    $children .= wp_list_pages("title_li=&child_of=".$post->ID ."&echo=0");
}
?>
<?php // Check to see if we have anything to output ?>
<?php if ($children) { ?>
<ul class="submenu">
    <?php echo $children; ?>
</ul>
<?php } ?>
0

wp_nav_menu css .

0

Source: https://habr.com/ru/post/1764972/


All Articles