I have a client site that requires the side menu to act like an accordion with the sub-menu's dropping down after the parent is clicked and staying open while the parent or any of the sub-menu's is active. The menu is created in PHP with the content for the buttons taken from the service section.
The menu is part of the custom theme and has been inserted into several page templates. As is part of the theme I would like to avoid changing the template php code. (not sure how often the theme will be updated) I have tried css and jQuery but so far have only managed to keep the sub-menu open briefly after the onclick then disappears when page loads.
You can see the site at https://www.birchandco.com - There is a hover state at the moment so that visitors can navigate. This would be removed once is working properly with an onclick.
PHP Template:
<div class="home_services">
<?php
$ser= new WP_Query(array(
'post_type'=>'service',
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
)
)
));
?>
<ul class="service_list">
<?php
if($ser->have_posts()) :
while($ser->have_posts()) : $ser->the_post();
?>
<li class="item">
<a href="<?php the_permalink(); ?>"><?php the_title();?> <i class="fa fa-angle-double-right" aria-hidden="true"></i></a>
<?php if(get_the_terms(get_the_ID() , 'service_category')[0]->term_id) {?>
<ul class="submenu">
<?php
$args = array(
'post_type' =>'service',
'orderby' => 'name',
'order' => 'ASC',
'post__not_in' => array(get_the_ID()),
'tax_query' => array(
array(
'taxonomy' => 'service_category',
'field' => 'term_id',
'terms' => get_the_terms(get_the_ID() , 'service_category')[0]->term_id
)
)
);
$query = new WP_Query( $args );
$loop = new WP_Query( $args );
if( $loop->have_posts() ):
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile; wp_reset_postdata();
endif;
?>
</ul>
<?php } ?>
</li>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
</ul>
</div>
CSS
This is the CSS for the current hover state for the submenu
.home_services ul.service_list > li:hover ul.submenu{
display: block;
opacity:1;
visibility:visible;}
JQuery
This is the code that I managed to get to work but it only toggles the sub-menu. Once the onclick event has been executed and the page loads then the sub-menu disappears.
jQuery(document).ready(function($){
var body = $('body');
$('.home_services ul.service_list li a').on('click',
function(event){
event.preventDefault();
// create accordion variables
var accordion = $(this);
var accordionContent = accordion.next('.home_services
ul.service_list > li ul.submenu');
// toggle accordion link open class
accordion.toggleClass("open");
// toggle accordion content
accordionContent.slideToggle(250);
});
});
Any help will be most appreciated.
I have seen a possible jquery solution but I am not sure how to apply the below code to my situation. If you can help it would be very much appreciated.
$('.home_services ul.service_list li a').click(function(e){
...
localStorage.setItem("activeSubMenu", $(this).text());
});
On page load, read the localStorage and expand the menu (if found):
$(document).ready(function(){
var activeItem = localStorage.getItem("activeSubMenu");
if(activeItem){
$('.home_services ul.service_list li a').filter(function() {
return $(this).text() == activeItem;
}).slideToggle();
}
});