Tagged: menu, Parent page, sidebar
-
AuthorPosts
-
February 20, 2018 at 10:27 pm #915080
Hi, I would like to add Parent page to sidebar navigation. Especially in case of automatically-generated list of pages in sidebar, when pages are sub-pages of a parent page, it would help to have parent page title (which will be active link leading to that page) above the sub-pages.
I found one thread about it already (see the link below) and a posted code from moderator. The code works, great, BUT it works good for cases, where PARENT page has only one level of sub-pages. So the structure is PARENT (1st level) , CHILD (2nd level).
As soon as I have two levels of sub-pages – not only children, but GRAND-CHILDREN as well, than whenever I click on a GRAND-CHILD sub-page in Sidebar, the Sidebar title changes to a 2nd level sub-page, which is a parent to that particular grand child.I would love to have PARENT (the first level) page as a title all the time (which will behave as active link to that page).
Here is a link to closed thread I mentioned: https://kriesi.at/support/topic/add-titles-to-sidebar-navigation/
And here is the original code for PARENT – CHILD menu (from Ismael), if anyone can help with changing it to my needs, it would be great :)
PS- I am using Enfold child theme
function avia_sidebar_menu($echo = true)
{
$sidebar_menu = “”;$subNav = avia_get_option(‘page_nesting_nav’);
$the_id = @get_the_ID();
$args = array();
global $post;if($subNav && $subNav != ‘disabled’ && !empty($the_id) && is_page())
{
$subNav = false;
$parent = $post->ID;
$sidebar_menu = “”;if (!empty($post->post_parent))
{
if(isset($post->ancestors)) $ancestors = $post->ancestors;
if(!isset($ancestors)) $ancestors = get_post_ancestors($post->ID);
$root = count($ancestors)-1;
$parent = $ancestors[$root];
}$args = array(‘title_li’=>”, ‘child_of’=>$parent, ‘echo’=>0, ‘sort_column’=>’menu_order, post_title’);
//enables user to change query args
$args = apply_filters(‘avia_sidebar_menu_args’, $args, $post);//hide or show child pages in menu – if the class is set to ‘widget_nav_hide_child’ the child pages will be hidden
$display_child_pages = apply_filters(‘avia_sidebar_menu_display_child’, ‘widget_nav_hide_child’, $args, $post);$children = wp_list_pages($args);
if ($children)
{
$default_sidebar = false;
$sidebar_menu .= “<nav class=’widget widget_nav_menu $display_child_pages’><ul class=’nested_nav’>”;
$sidebar_menu .= $children;
$sidebar_menu .= “</nav>”;
}
}$sidebar_menu = apply_filters(‘avf_sidebar_menu_filter’, $sidebar_menu, $args, $post);
if($echo == true) { echo $sidebar_menu; } else { return $sidebar_menu; }
}- This topic was modified 6 years, 9 months ago by siven111.
February 28, 2018 at 8:10 am #918844Hey siven111,
Thank you for using Enfold.
Please replace the modification with the following code.
$default_sidebar = false; $parenttitle = get_the_title($parent); $parentlink = get_the_permalink($parent); $sidebar_menu .= "<nav class='widget widget_nav_menu $display_child_pages'><ul class='nested_nav'>"; $sidebar_menu .= "<a href='{$parentlink}'>$parenttitle</a>"; $sidebar_menu .= $children; $sidebar_menu .= "</ul></nav>";
We would like to apologize for the late response. I have no idea why I didn’t see the thread immediately.
Best regards,
Ismael -
AuthorPosts
- You must be logged in to reply to this topic.