Skip to main content
Edited question to better correspond with accepted answer
Link

How to use get_terms() in extendeddynamically add custom taxonomy terms as a sub-menu of an existing menu item, using custom walker class

Include new discoveries & edited the issue & the strategy.
Source Link

The issue I've encountered is that when I try to call the get_terms() function inside the custom walker, it returns "Invalid taxonomy".

What I've tried

I read up about it,know for a fact that the taxonomy is registered and I think it's due tothat there are terms in the database. It seems like the get_terms() is getting called before init when it's invoked in the walker class. Since the taxonomy gets registered on init, the taxonomy is invalid when the walker class is calling get_terms();

What I've tried

So I tried to lift out the get_terms() function from the class and do the following:

I even tried making the variable global, but it still doesn't return any terms, and throws an error for the taxonomy being invalid. But aA var_dump on that variable still gives me "Invalid taxonomy" error. So I read on, and saw in a comment at the codex (on the Walker_Nav_menu class page) that I need to use static functions, and did this inside the custom walker class:

And thatThat var_dump runs as it should but still returns the "Invalid taxonomy" error.

Edit

I know for a fact thattried changing my custom taxonomy to the built-in taxonomy is registered'category' and that there are terms in the databasethen my function works. Using 'mytax' it still gives me invalid taxonomy.

The custom taxonomy is registered with

add_action( 'init', 'custom_register_taxonomies');

I am now fairly certain that this issue is due to the wp_nav_menu call coming earlier than the registration of my custom taxonomy.

The questionquestions

So what am I missing hereIs it safe to register the custom taxonomies earlier than on init? I'm including my

Am I right in using static functions in the custom walker class, if anyone would likeas described above?

Would it improve anything to show me how I would best go aboutdelay the wp_nav_menu call, by calling it from a custom location in my child theme. Rather than letting the get_terms() function inside thisparent theme call it, and adapting my walker class. and/or taxonomy registration function?

Am I on the right track here or am I missing something else?

I'm including my custom walker below. Any tips are greatly appreciated.

The issue I've encountered is that when I try to call the get_terms() function inside the custom walker, it returns "Invalid taxonomy".

What I've tried

I read up about it, and I think it's due to the get_terms() is getting called before init when it's invoked in the walker class. So I tried to lift out the get_terms() function from the class and do the following:

I even tried making the variable global, but it still doesn't return any terms, and throws an error for the taxonomy being invalid. But a var_dump on that variable still gives me "Invalid taxonomy". So I read on, and saw in a comment at the codex (on the Walker_Nav_menu class page) that I need to use static functions, and did this inside the custom walker class:

And that var_dump runs as it should but still returns the "Invalid taxonomy" error. I know for a fact that the taxonomy is registered and that there are terms in the database.

The question

So what am I missing here? I'm including my custom walker, if anyone would like to show me how I would best go about calling the get_terms() function inside this class.

The issue I've encountered is that when I try to call the get_terms() function inside the custom walker, it returns "Invalid taxonomy". I know for a fact that the taxonomy is registered and that there are terms in the database. It seems like the get_terms() is getting called before init when it's invoked in the walker class. Since the taxonomy gets registered on init, the taxonomy is invalid when the walker class is calling get_terms();

What I've tried

So I tried to lift out the get_terms() function from the class and do the following:

I even tried making the variable global, but it doesn't return any terms, and throws an error for the taxonomy being invalid. A var_dump still gives me "Invalid taxonomy" error. I read on, and saw in a comment at the codex (on the Walker_Nav_menu class page) that I need to use static functions, and did this inside the custom walker class:

That var_dump runs but still returns the "Invalid taxonomy" error.

Edit

I tried changing my custom taxonomy to the built-in taxonomy 'category' and then my function works. Using 'mytax' it still gives me invalid taxonomy.

The custom taxonomy is registered with

add_action( 'init', 'custom_register_taxonomies');

I am now fairly certain that this issue is due to the wp_nav_menu call coming earlier than the registration of my custom taxonomy.

The questions

Is it safe to register the custom taxonomies earlier than on init?

Am I right in using static functions in the custom walker class, as described above?

Would it improve anything to delay the wp_nav_menu call, by calling it from a custom location in my child theme. Rather than letting the parent theme call it, and adapting my walker class and/or taxonomy registration function?

Am I on the right track here or am I missing something else?

I'm including my custom walker below. Any tips are greatly appreciated.

Changed from get_permalink to get_term_link.
Source Link
/* ---- Add mytax taxonomy terms to menu ---- */
class Walker_Add_Myterms extends Walker_Nav_Menu {
  // Insert a submenu
  function end_el( &$output, $item, $depth=1, $args=array() ) {
      // if the current menu item being output is parentmenuitem
      if( 'parentmenuitem' == $item->title ){
          // get all terms
          $myterms = get_terms( array(
              'taxonomy' => 'mytax',
              'hide_empty' => true,
          ) );
          if ( ! empty( $myterms ) && ! is_wp_error( $myterms ) ) {
              // start a new list
              $output .= '<ul>';
              // iterate over each type and add an li
              foreach( $myterms $myterm){
                  $term_url = get_permalinkget_term_link( $myterm->term_id , 'mytax');
                  $name = $myterm->name;
                  $format = '<li><a href="%s">%s</a></li>';
                  $output .= sprintf( $format, $term_url, $name );
              }
              // close the list
              $output .= '</ul>';
          }
      }
      // close the parent li
      $output .= "</li>\n";  
  }
}
wp_nav_menu( array(
    'theme_location'  => 'expanded',
    'container'       => 'ul',
    'menu_class'      => 'expanded-menu sub-menu active',
    'walker'          => new Walker_Add_Myterms
) );
/* ---- Add mytax taxonomy terms to menu ---- */
class Walker_Add_Myterms extends Walker_Nav_Menu {
  // Insert a submenu
  function end_el( &$output, $item, $depth=1, $args=array() ) {
      // if the current menu item being output is parentmenuitem
      if( 'parentmenuitem' == $item->title ){
          // get all terms
          $myterms = get_terms( array(
              'taxonomy' => 'mytax',
              'hide_empty' => true,
          ) );
          if ( ! empty( $myterms ) && ! is_wp_error( $myterms ) ) {
              // start a new list
              $output .= '<ul>';
              // iterate over each type and add an li
              foreach( $myterms $myterm){
                  $term_url = get_permalink( $myterm->term_id );
                  $name = $myterm->name;
                  $format = '<li><a href="%s">%s</a></li>';
                  $output .= sprintf( $format, $term_url, $name );
              }
              // close the list
              $output .= '</ul>';
          }
      }
      // close the parent li
      $output .= "</li>\n";  
  }
}
wp_nav_menu( array(
    'theme_location'  => 'expanded',
    'container'       => 'ul',
    'menu_class'      => 'expanded-menu sub-menu active',
    'walker'          => new Walker_Add_Myterms
) );
/* ---- Add mytax taxonomy terms to menu ---- */
class Walker_Add_Myterms extends Walker_Nav_Menu {
  // Insert a submenu
  function end_el( &$output, $item, $depth=1, $args=array() ) {
      // if the current menu item being output is parentmenuitem
      if( 'parentmenuitem' == $item->title ){
          // get all terms
          $myterms = get_terms( array(
              'taxonomy' => 'mytax',
              'hide_empty' => true,
          ) );
          if ( ! empty( $myterms ) && ! is_wp_error( $myterms ) ) {
              // start a new list
              $output .= '<ul>';
              // iterate over each type and add an li
              foreach( $myterms $myterm){
                  $term_url = get_term_link( $myterm->term_id , 'mytax');
                  $name = $myterm->name;
                  $format = '<li><a href="%s">%s</a></li>';
                  $output .= sprintf( $format, $term_url, $name );
              }
              // close the list
              $output .= '</ul>';
          }
      }
      // close the parent li
      $output .= "</li>\n";  
  }
}
wp_nav_menu( array(
    'theme_location'  => 'expanded',
    'container'       => 'ul',
    'menu_class'      => 'expanded-menu sub-menu active',
    'walker'          => new Walker_Add_Myterms
) );
Source Link
Loading