Three levels category menu
-
Hi everyone,
I am totally lost…
I am trying to build a three levels menu based on categories.
The problem is that it has to be built in a way that the owner of the site will be able to add categories and custom post types on the frontend without going to the appearance -> menus section.
I use wp_list_categories to display the parent categories. Then, I am using get_the_category to to display categories list outside the loop. Last level, I use get_posts to retrieve posts info.
Firstly, it doesn’t work. I only get parent categories and posts.
Secondly, this scheme won’t allow me to add a css class to active items and so give the frontend user the ability to know where he is.
Does anyone has an idea of how to build such a menu system in WordPress?
Thanks!Here is my code :
<div class="navigation col-lg-4 col-md-4 col-xs-4"> <div class="col-lg-4 col-md-4 col-xs-12"> <?php $args = array( 'show_option_all' => '', 'orderby' => 'name', 'order' => 'ASC', 'style' => 'none', 'show_count' => 0, 'hide_empty' => 0, 'use_desc_for_title' => 1, 'child_of' => 0, 'feed' => '', 'feed_type' => '', 'feed_image' => '', 'exclude' => 1, 'exclude_tree' => '', 'include' => '', 'hierarchical' => 1, 'title_li' => __( 'Categories' ), 'show_option_none' => __( 'No categories' ), 'number' => null, 'echo' => 1, 'depth' => 1, 'current_category' => 0, 'pad_counts' => 0, 'taxonomy' => 'category', 'walker' => null ); wp_list_categories( $args ); ?> </div> <div class="col-lg-4 col-md-4 col-xs-12"> <?php $categories = get_the_category(); if ($categories) { $category_id = $categories[0]->cat_ID; } else { $all_cats = get_categories(); foreach ($all_cats as $value) { if(is_category( $value->slug )) { $category_id = $value->cat_ID; } } } if (isset($category_id)): $category_id = get_the_category(); $separator = ' '; $output = ''; foreach ($category_id as $category_link) { $output .= '<a href="'.get_category_link( $category_link->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category_link->name ) ) . '">'.$category->cat_name.'</a>'.$separator; } echo trim($output, $separator); $videos = array( 'posts_per_page' => 99, 'post_type' => 'video', 'orderby' => 'menu_order', 'order' => 'DESC' ); $cat_posts = get_posts( $videos ); foreach ( $cat_posts as $post ) : setup_postdata( $post ); ?> <li><a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </li> <?php endforeach; wp_reset_postdata(); endif; ?> </div> <div class="col-lg-4 col-md-4 col-xs-12"> </div> </div>
- The topic ‘Three levels category menu’ is closed to new replies.