I want to add a submenu of a wordpress menu into my theme. I want to use the wp_nav_menu function of Wordpress 3.0. And in other words, I want to see the submenu not the subpages which means that wp_list_pages is not the right function because I want the submenu and not the subpages.
Let's assume the menu structure looks like that:
Home
Entry1
Entry3
Entry4
Entry2
Entry5
Entry6
I want that if someone clicks on Entry1 (and makes it the parent) the Theme just shows the submenu of this entry. In the case of Entry1 it's:
Entry3
Entry4
I know that there is a code like that:
<?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 point is that I'm talking about the menu structure and not the page structure. Oh, and the depth parameter does not work because it means to here and not from here.
I think there could be a solution with a custom walker but I don't know how to implement that.
Function reference for wp_nav_menu
http://codex.wordpress.org/Template_Tags/wp_nav_menu
I'm looking for a solution for this problem for so long so please help me. Thanks a lot.
In order to get this to work I had to hide the .sub-menu as soon as the page loaded. Then, show only the relevant sub-menu by targeting ".current_page_item .sub-menu"
$(document).ready(function() {
$(".sub-menu").hide(); // hide the submenu on page load
$(".current_page_item .sub-menu").show();
)};
This should help: From 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 } ?>
One solution is to put another wp_nav_menu function on page and to modify css to hide inactive menu items.
Related
I am attempting to show categories in a list, but without displaying subcatgories. My current code is:
<?php if (is_category()) {
$this_category = get_category($cat);
if (get_category_children($this_category->cat_ID) != "") {
echo "<ul>";
wp_list_categories('orderby=id&show_count=0&title_li=
&use_desc_for_title=1&child_of='.$this_category->cat_ID);
echo "</ul>";
}
}?>
Which displays the categories nicely
but when I added a sub-category it looked like this:
Any ideas? Thanks!
Use get_categories() instead.
https://developer.wordpress.org/reference/functions/get_categories/#Get_only_top_level_categories
It has "parent" parameter which you can set to 0 and get desired result.
looks like a css problem to me : you probably want to hide the subcategory item until it's clicked or hovered
I have just noticed that my menu items id are not getting generated by wordpress.
I expect it to be something like this
<li id='menu-item-2091' class='menu-item menu-item-209'>
<a href='www.mysite.com/members/<?php echo $user_info->user_login; ?>'>Profile</a></li>
but i get this without the menu item id.
<li class='menu-item menu-item-209'>
<a href='www.mysite.com/members/<?php echo $user_info->user_login; ?>'>Profile</a></li>
This error is occurring in my main menu, but my top menu seems to generate the menu item id properly.
I have looked around the menus area and can't find any clues why??
Here is a filter that hacks it out
add_filter ('wp_nav_menu_items','gfb_missing_id_fix', 10, 2);
function gfb_missing_id_fix($menu, $args) {
if($args->theme_location == "primary-menu"){
$dom = new DOMDocument;
$dom->loadHTML($menu);
foreach($dom->getElementsByTagName('li') as $element ) {
$classes = $element->getAttribute("class");
preg_match("/menu-item-\d+/", $classes, $output_array);
$element->setAttribute("id", $output_array[0]);
}
$menu = $dom->saveHTML();
}
return $menu;
}
General info
I use my navigation twice (mobile and desktop with different menu ids) and only the second menu is missing the item ids. This makes sense, because it secures that menu-item-ids are only used once.
JQuery Solution
For me the following javascript / jquery solution does the trick. It adds menu-id-ID (with ID as the actual menu-item-id) to each menu item.
var $mainMenu = $("#main-nav"); // your menu_id used in wp_nav_menu
$mainMenu.find("li").each(function(index, element) {
var $element = $(element);
var classes = $element.attr("class"),
ID = classes.match(/menu-item-(\d+)/)[1];
$element.attr("id", "menu-id-" + ID); // here 'menu-id-' is prepended to the actual ID
});
I'm creating a wordpress theme and I'm looking to show the sub nav only if the current page has children, I'm using the code below which works great, but doesn't have an if statement, which means it shows an empty menu on some pages.
<?php global $id;
wp_list_pages("title_li=&child_of=$id&show_date=modified&date_format=$date_format");
?>
Try below :-
$kids = get_pages("child_of=$id");
if (count($kids) > 0) { wp_list_pages(...); }
I have a navigation structure like this:
Home
About
The Team
Our Mission
Locations
Chicago
New York
Los Angeles
Services
Contact Us
If I'm looking at the About page, I want the sidebar to display:
The Team
Our Mission
If I'm looking at the Services page, I want the sidebar to display:
Home
About
Locations
Services
Contact Us
This is the code I'm currently using. How can I modify it so that it works the way I want?
<?php
global $wp_query;
if( empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->ID;
} else {
$parent = $wp_query->post->post_parent;
}
$children = get_pages('child_of='.$parent);
if( count( $children ) > 0 ) {
?>
<ul>
<?php wp_list_pages ("&title_li=&child_of=$parent"); ?>
</ul>
<?php } ?>
this should either do it outright or at least show you how:
when viewing a Page that has children (or is a child) it displays only children of that parent.
When visiting main page, all top level pages are listed in the sidebar.
When visiting a top level page with no children, all top level pages are listed.
When visiting a top level page with children, just the children pages, and descendant pages, are listed.
When visiting a child page, just the children, and descendant pages, of that parent, are listed.
<?php
$output = wp_list_pages('echo=0&depth=1&title_li=<h2>Top Level Pages </h2>' );
if (is_page( )) {
$page = $post->ID;
if ($post->post_parent) {
$page = $post->post_parent;
}
$children=wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=' );
if ($children) {
$output = wp_list_pages ('echo=0&child_of=' . $page . '&title_li=<h2>Child Pages</h2>');
}
}
echo $output;
?>
from: http://codex.wordpress.org/Function_Reference/wp_list_pages#List_subpages_even_if_on_a_subpage
This method takes advantage of the echo=0 parameter, which returns the results as a string to a variable. The if ($children) test now works because wp_list_pages() will return empty if it finds no matches.
Ok, this is really bugging me! I wrote a function (well, modified a very similar one from the wordpress codex) which lists all child pages and the parent page whenever the page has children or is a child. That bit works fine, but what I want to do now is have it look in a custom field (add-to-nav) which has the path to another page to add to the end of the list. It checks the page and parent for that custom field.
It seems to be echoing all the right details but for some reason won't add the page to the list. Would really appreciate any help!
function add_sec_nav_w_extras(){
global $post;
global $wpdb;
$key = 'add-to-nav';
$addition = get_post_meta($post->ID, $key, TRUE);
if($addition == '') {$addition = get_post_meta($post->post_parent, $key, TRUE);}
if($addition != '') {
$addition_page = get_page_by_path($addition);
}
echo("Addition page: ".$addition_page->post_title."<br/>Addition ID: ".$addition_page->ID."<br/>Current: ".$post->ID."<br/>Parent: ".$post->post_parent);
?><section id="secondnav_w_parent" class="fix"><ul><?php
if($post->post_parent != "0") { // If post has parent, list that parent and all its children
wp_list_pages( array('title_li'=>'','include'=>$post->post_parent) );
wp_list_pages( array('title_li'=>'','depth'=>1,'child_of'=>$post->post_parent) );
if($addition_page != ''){
wp_list_pages( array('title_li'=>'','include'=>$addition_page->ID) );
}
}
else if(get_pages('child_of='.$post->ID)) { // If post is a parent, list itself and all children
wp_list_pages( array('title_li'=>'','include'=>$post->ID) );
wp_list_pages( array('title_li'=>'','depth'=>1,'child_of'=>$post->ID) );
if($addition_page != ''){
wp_list_pages( array('title_li'=>'','include'=>$addition_page->ID) );
}
}
?></ul></section><?php
}
EDIT:
Ok, so now I know why it's not showing up... I told it not to! I'm using the exclude pages plugin so that the page doesn't show up in the primary nav, and that apparently affects anything that uses wp_list_pages. So now I need to find a way to force it in there... Any thoughts?
EDIT2:
Have now disables the exclude pages plugin and using a custom menu for the primary nav. Bit of a pain but I guess not too bad. If anyone's got a better suggestion let me know!