WordPress.org

Ready to get started?Download WordPress

Forums

Dynamic Post Category Menu (5 posts)

  1. padreed123
    Member
    Posted 1 year ago #

    I have alot of Post Categories and I don't want to have to keep manually adding them to my drop down menu. Whats the best way to Dynamicly add post categories to the menu? Is there a plugin?

  2. NewtonPlease
    Member
    Posted 1 year ago #

  3. padreed123
    Member
    Posted 1 year ago #

    Close.. But I need it to add the hierarchy of parents and children automatically. That plugin would be perfect if it did that. That way i could just add the top categories of my liking and all of those categories children would dynamically add themselves. Then I could stick with my themes css too and not use a menu plugin that replaces the whole menu.

    I've searched but still no luck.. maybe I'll have to write it myself :'(.. Boy WordPress has spoiled me.

    - Thx and thx ahead for the help.

  4. NewtonPlease
    Member
    Posted 1 year ago #

    I think you are going to have to write that. Have fun!

  5. padreed123
    Member
    Posted 1 year ago #

    Here what I found that works. You have to create a menu walker.

    add this to your themes functions.php:

    add_action('wp_loaded','register_nav_menu_class');
    
    function register_nav_menu_class(){
    
    	class Custom_Walker_Nav_Menu extends Walker_Nav_Menu  {
    
    	    function start_el(&$output, $item, $depth, $args) {
    
    		global $wp_query;
    
    		$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    
    		$class_names = $value = '';
    
    		$classes = empty( $item->classes ) ? array() : (array) $item->classes;
    
    		$classes[] = 'menu-item-' . $item->ID;
    
    		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
    
    		$class_names = ' class="' . esc_attr( $class_names ) . '"';
    
    		$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
    
    		$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
    
    		$output .= $indent . '<li' . $id . $value . $class_names .'>';
    
    		$attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
    
    		$attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
    
    		$attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
    
    		$attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
    
    		$item_output = $args->before;
    
    		$item_output .= '<a'. $attributes .'>';
    
    		$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    
    		$item_output .= '</a>';
    
    	    if($item->object == 'category'){  	
    
    			$child_cats = wp_list_categories('title_li=&echo=0&child_of='.$item->object_id);
    
    			if( $child_cats ){
    
    				$item_output .= '<ul class="sub-menu">' .$child_cats. '</ul>';;		
    
    			}			
    
    		}
    
    		$item_output .= $args->after;
    
    		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    
    	    }
    
    	}
    
    }

    Then add this to your themes header.php:

    <?php
     			wp_nav_menu(array(
    	 			'container' => '',
    	 			'container_class' => '',
    	 			'menu_class' => 'dropdown',
    	 			'menu_id' => 'nav',
    	 			'sort_column' => 'menu_order',
    	 			'theme_location' => 'primary',
    				'walker' => new Custom_Walker_Nav_Menu()
    	 			));
    ?>

    *Reference: http://wpquestions.com/question/show/id/1480

Topic Closed

This topic has been closed to new replies.

About this Topic