• Resolved Agence Myso

    (@agence-myso)


    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>

Viewing 7 replies - 16 through 22 (of 22 total)
  • Thread Starter Agence Myso

    (@agence-myso)

    Hi and happy new year!
    How funny it is, I was just thinking I would rename my single.php in single-video.php just to try!

    Thread Starter Agence Myso

    (@agence-myso)

    Arg… I can’t get it to work…
    I’ve tried to add a single-video.php file, but it does not solve anything… Each time I click on a video file, my menu disappears, as if I was at the root level of the site…

    What I would do to troubleshoot this is print out how your menu code is being executed. For example, add an echo statement inside an if statement, add an else and echo something there. Do this for every logical break and for every piece of data returned.

    Compare those results to the page that is working. This should show you what’s changed in the flow and give you an idea of what might need to be tweaked.

    Thread Starter Agence Myso

    (@agence-myso)

    Thank you for this smart answer.
    If I do an if on this :

    if( is_category() || is_single() ) {
      			$cat = get_query_var('cat');
      			$category = get_category( $cat );
    			if ( is_category(26) || $category->category_parent == 26 ):

    and then load the category menu and the video post types, everything works until I get to the single template (I click on a video).
    Once I am on the ‘single’ template, I get this error :

    NOTICE: UNDEFINED PROPERTY: WP_ERROR::$CATEGORY_PARENT IN /APPLICATIONS/MAMP/HTDOCS/SUPERGRAFIC/WP-INCLUDES/CLASS-WP-ERROR.PHP ON LINE 78

    There is something I don’t understand in the wp system…
    Can’t it be a problem linked to the fact that I am working with custom post types?

    Thread Starter Agence Myso

    (@agence-myso)

    Ok, I found a solution.
    It is impossible to call for get_category in a custom post type single template.
    So I made an else if after the first ìf ( is_category() )`.
    The only problem remaining is that it is very heavy (I have to paste the whole piece of code concerning wp_nav_menu and get_post.
    Any idea on how to lighten it?

    You can wrap all the code you used in a custom function and then call that function whenever needed, passing any arguments that may be different.

    Thread Starter Agence Myso

    (@agence-myso)

    Man, it was so easy!
    Thank you for your help.
    Now everything works perfectly and I can add my menus in a very simple way! That’s so cool!

Viewing 7 replies - 16 through 22 (of 22 total)
  • The topic ‘Three levels category menu’ is closed to new replies.