• Resolved steighne

    (@steighne)


    Hi,
    I am working with a client and would like that every time they add a category, it is automatically added as a sub-menu item in the Primary navigation menu. The categories are in a custom post type with ‘project’ as the taxonomy name. I’d like to avoid a plugin if possible.

    I’ve been poring through wp’s filters and am struggling to come up with a working code. It seems like a simple li loop with get_items() but I am stuck. Any help on this would be greatly appreciated.

    Richard

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • You can use the wp_nav_menu_objects filter. Try this (haven’t tested it yet):

       add_filter( 'wp_nav_menu_objects', 'wpg2byt_add_term_parent' );
        function wpg2byt_add_term_parent( $items ) {
          $terms = get_terms( array(
            'taxonomy' => 'project',
            'hide_empty' => false,
         ) );
      
          foreach ($terms as $term) {
          //format its data to be compatible with the filter
          $link = array (
                'title'            =>  $term->name,
                'menu_item_parent' => //id of the parent menu,
                'ID'               => '',
                'db_id'            => '',
                'url'              => get_term_link($term)
            );
          $items[] = (object) $link;
          }
          return $items;    
        }
    • This reply was modified 4 years, 4 months ago by Joms.
    Thread Starter steighne

    (@steighne)

    Hi Joms, this did the trick. Added a couple things (for anybody else who might want to use this) including grabbing the term_id and appending the classes. I appreciate the help!
    Richard

    add_filter( 'wp_nav_menu_objects', 'wpg2byt_add_term_parent' );
        function wpg2byt_add_term_parent( $items ) {
          $terms = get_terms( array(
            'taxonomy' => 'project',
            'hide_empty' => false,
        ) );
          foreach ($terms as $term) {
          //format its data to be compatible with the filter
          $link = array (
                'title'            => $term->name,
                'menu_item_parent' => //id of the parent menu,
                'ID'               => $term->term_id,
                'db_id'            => '',
                'url'              => get_term_link($term),
                'classes'          => array( 'my-classes' )
            );
          $items[] = (object) $link;
          }
          return $items;   
        }
    • This reply was modified 4 years, 4 months ago by steighne.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Automatically add CPT category as Nav menu sub item’ is closed to new replies.