2

I've been googling around about days working for a client and would like that every time they add a product_category, it should get automatically added as a sub-menu item in the Primary navigation menu but still had no luck.

Briefly I want to have a dynamically generated menu of my all product_categories.

I've tried filtering through this code:

function new_nav_menu_items($items, $args) {

$args = array(
            'taxonomy'   => "product_cat",
            'hide_empty' => true,
            'hierarchical' => 1,
        );

$product_categories = get_terms($args);

         foreach($product_categories as $sub_category) {
            $items = $items . '<li class="menu-item"><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>';
        } 
    
        return $items;
}

add_filter( 'wp_nav_menu_items', 'new_nav_menu_items', 10, 2);

But I couldn't implement hierarchically order and sub-menus. Any ideas how should I reach this goal?

1 Answer 1

1

put this on your functions.php and then call the function in whatever page desired. this code is prepare to use css.

if (! function_exists('menu_categories')) {
  function menu_categories(){
    $cat_args = array( 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => true);
    $cats = get_terms( 'product_cat', $cat_args ); ?>
    <h1>Categories</h1><?php
    foreach ($cats as $key => $cat):
      if ($cat->parent == 0): ?>
        <div class="tc-menuint-1">
          <a href="<?php echo get_term_link($cat) ?>"><?php echo $cat->name; ?></a></li>
          <div class="tc-menuint-2"><?php
            foreach ($cats as $key => $cat2):
              if ($cat2->parent == $cat->term_id): ?>
                <div class="tc-menuint-3">
                  <li><a href="<?php echo get_term_link($cat2) ?>"><?php echo $cat2->name; ?></a></li>
                  <div class="tc-menuint-4"> <?php
                    foreach ($cats as $key => $cat3):
                      if ($cat3->parent == $cat2->term_id): ?>
                        <li><a href="<?php echo get_term_link($cat3) ?>"><?php echo $cat3->name; ?></a></li><?php
                      endif;
                    endforeach; ?>
                  </div>
                </div> <?php
              endif;
            endforeach; ?>
          </div>
        </div><?php
      endif;
    endforeach;
  }
}

Then from anywhere we can call the function.

<?php menu_categories(); ?> 

Well is that!!

To understand the php code, we are getting all the categories like so:

$cat_args = array( 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => true);
$cats = get_terms( 'product_cat', $cat_args );
foreach ($cats as $key => $cat):
  echo '<a href='.get_term_link($cat).'>'.$cat->name.'</a><br>';
endforeach;

Once all the categories and subcategories are showing: we can filter it to show only the categories (top level):

$cat_args = array( 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => true);
$cats = get_terms( 'product_cat', $cat_args );
foreach ($cats as $key => $cat):
  if ($cat->parent == 0):
    echo '<a href='.get_term_link($cat).'>'.$cat->name.'</a><br>';
  endif;
endforeach;

If we want the sub-categories we can filter them again:

$cat_args = array( 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => true);
$cats = get_terms( 'product_cat', $cat_args );
foreach ($cats as $key => $cat):
  if ($cat->parent == 0):
    echo '<a href='.get_term_link($cat).'>'.$cat->name.'</a><br>';

    foreach ($cats as $key => $cat2):
      if ($cat2->parent == $cat->term_id):
        echo '<a href='.get_term_link($cat2).' style="color:red;">'.$cat2->name.'</a><br>';
      endif;
    endforeach;

  endif;
endforeach;

then if we want a sub-sub-categorie, we can add another block of code like the one before changing in this case $cat2 to for example $cat3, and compare it to the term_id of the parent $cat2. Now if for some reason we need more sub-categories we can go on and on...

$cat_args = array( 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => true);
$cats = get_terms( 'product_cat', $cat_args );
foreach ($cats as $key => $cat):
  if ($cat->parent == 0):
    echo '<a href='.get_term_link($cat).'>'.$cat->name.'</a><br>';
    foreach ($cats as $key => $cat2):
      if ($cat2->parent == $cat->term_id):
        echo '<a href='.get_term_link($cat2).' style="color:red;">'.$cat2->name.'</a><br>';
        
        foreach ($cats as $key => $cat3):
          if ($cat3->parent == $cat2->term_id):
            echo '<a href='.get_term_link($cat3).' style="color:green;">'.$cat3->name.'</a><br>';
          endif;
        endforeach;

      endif;
    endforeach;
  endif;
endforeach;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.