Skip to content

Instantly share code, notes, and snippets.

@ridhamdholakia
Last active April 16, 2018 06:14
Show Gist options
  • Save ridhamdholakia/e6c779ed0af9274d2686c9c4c7b12141 to your computer and use it in GitHub Desktop.
Save ridhamdholakia/e6c779ed0af9274d2686c9c4c7b12141 to your computer and use it in GitHub Desktop.
Wordpress Accordian Menu
function sidebar_accordian_left()
{
global $post;
$categories = get_terms( 'education_categories', array(
'hide_empty' => false,
) );
echo '<ul class="accordian-menu" id="toggle-view">';
foreach( $categories as $category ) {
$cat_id = $category->term_id;
$category_link = sprintf(
'<a href="%1$s" class="clicka" alt="%2$s">%3$s<span>+</span></a>',
esc_url( '#' ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name )
);
$args = array(
'post_type' => 'education_blog',
'posts_per_page' => -1,
'offset'=> 1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'education_categories',
'field' => 'term_id',
'terms' => $cat_id,
),
),
);
$myposts = get_posts( $args );
$queried_object = get_queried_object();
$page_title = $queried_object->post_name;
$post_id = $queried_object->ID;
$terms = wp_get_post_terms( $post_id , 'education_categories' , array("fields" => "all") );
foreach ($terms as $value) {
$post_tax = $value->slug;
}
echo '<li id="'.$category->slug.'" class="main-accordian-li">' . sprintf( esc_html__( '%s', 'textdomain' ), $category_link ) ?>
<div class="panel"<?php if($category->slug == $post_tax){ echo 'style="display:block;"';}else if($category->slug != $post_tax){echo 'style="display:none;"';} ?>><ul class="accordian-submenu">
<?php foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li <?php if($page_title == $post->post_name){ echo 'class="current-item-accordian"'; }?>>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach;
wp_reset_postdata();
?>
</ul></div>
<?php
echo '</li> ';
}
echo '</ul>';
}
add_shortcode('Leftbar_accordian','sidebar_accordian_left');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment