WordPress.org

Forums

put a post inside a menu (Walker_Nav_Menu) (1 post)

  1. muccach
    Member
    Posted 2 years ago #

    Hello everybody,
    I've got this idea but i don't get how to do it.

    I've made a menu (wp_nav_menu) create by big square, let say something like windows 8.
    obviously clicking the square will send me to the desired page, until this point no problem. But what I wont is to put a custom-post inside the button itself because it gave me the possibility to use different images and text for each button using custom-field.

    The idea is to tell the_walker to check that if the "Navigation Label" is equal to the post_title, then put the content of the post inside the square (through the walker).

    I know it sound quite strange and maybe not so easy to understand by I hope that somebody can help me.
    Thank you i advance

    Here the walker I'm trying use

    class menu_quadrati extends Walker_Nav_Menu
    {
    
        public function start_lvl( &$output, $depth )
        {
            $output .= '<ul id="men_bottoni">';
        }
    
        public 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;
    
               $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
               $class_names = ' class="'. esc_attr( $class_names ) . '"';
    
               $output .= $indent . '<li>';
    
               $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        ) .'"' : '';
    
               $prepend = '<strong>';
               $append = '</strong>';
    
              if($depth != 0)
               {
                         $description = $append = $prepend = "";
               }
               // This is the idea of what i'm trying to do
               $bottoni = new WP_Query( array( 'post_type' => 'gestione-bottoni') );//get the post
               foreach ($bottoni as $bottone)
               {
                if ($bottone->post_title == $item->title)
                  {
                    //put my custom-field inside the <a> of the menu itself
                  }
    
                }
    
                $item_output = $args->before;
                $item_output .= '<a'. $attributes .'>';
                $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
                $item_output .= $description.$args->link_after;
                $item_output .= '</a>';
                $item_output .= $args->after;
    
                $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    
    }

    Sorry I'm working locally so I can't put a page link

Topic Closed

This topic has been closed to new replies.

About this Topic