skip to Main Content

I have been trying to add else to my foreach loop where it shows categories and subcategories. But if there is no categories available then I want it to show a text where it tells that there is no categories or products in categories.

Right now it shows the text that there is no products in categories anyways even if there is some categories shown.

<?php

  $taxonomy     = 'product_cat';
  $orderby      = 'menu_order';  
  $show_count   = 0;      // 1 for yes, 0 for no
  $pad_counts   = 0;      // 1 for yes, 0 for no
  $hierarchical = 1;      // 1 for yes, 0 for no  
  $title        = '';  
  $empty        = 1;

  $args = array(
         'taxonomy'     => $taxonomy,
         'orderby'      => $orderby,
         'show_count'   => $show_count,
         'pad_counts'   => $pad_counts,
         'hierarchical' => $hierarchical,
         'title_li'     => $title,
         'hide_empty'   => $empty
  );
 $all_categories = get_categories( $args );
 
      echo '<ul>';
      echo '<div class="category"><h1>CATEGORIES</h1></div>';
 
 foreach ($all_categories as $cat) {
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id; 
        
         echo '<li class="parentcategory">';
        echo '<a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';
        echo '</li>';
        

        $args2 = array(
                'taxonomy'     => $taxonomy,
                'child_of'     => 0,
                'parent'       => $category_id,
                'orderby'      => $orderby,
                'show_count'   => $show_count,
                'pad_counts'   => $pad_counts,
                'hierarchical' => $hierarchical,
                'title_li'     => $title,
                'hide_empty'   => $empty
        );
        $sub_cats = get_categories( $args2 );
        if($sub_cats) {
            foreach($sub_cats as $sub_category) {
                echo '<li class="subcategory">';
                echo  '<a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>' ;
                echo '</li>';
            }   
        } 
    }    
else {
          echo '<li>';
          echo '<p>';
          echo 'Currently there is no categories made';
          echo ' or any products in the categories';
          echo '</p>';
          echo '</li>';

   } 
}

 echo '</ul>';
 
?>

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to @Doğuş I came up with this solution. I added a if( !empty($all_categories) ){ before foreach loop and it seems to be working like it's supposed to. It shows categories when there is some and where there isn't it shows the text.

    If anyone has a better solution please post your answer.

    <?php
    
      $taxonomy     = 'product_cat';
      $orderby      = 'menu_order';  
      $show_count   = 0;      // 1 for yes, 0 for no
      $pad_counts   = 0;      // 1 for yes, 0 for no
      $hierarchical = 1;      // 1 for yes, 0 for no  
      $title        = '';  
      $empty        = 1;
    
      $args = array(
             'taxonomy'     => $taxonomy,
             'orderby'      => $orderby,
             'show_count'   => $show_count,
             'pad_counts'   => $pad_counts,
             'hierarchical' => $hierarchical,
             'title_li'     => $title,
             'hide_empty'   => $empty
      );
     $all_categories = get_categories( $args );
     
          echo '<ul>';
          echo '<div class="categories"><h1>CATEGORIES</h1></div>';
     
     
      if( !empty($all_categories) ){
     
     foreach ($all_categories as $cat) {
        if($cat->category_parent == 0) {
            $category_id = $cat->term_id; 
            
             echo '<li class="parentcategory">';
            echo '<a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';
            echo '</li>';
            
    
            $args2 = array(
                    'taxonomy'     => $taxonomy,
                    'child_of'     => 0,
                    'parent'       => $category_id,
                    'orderby'      => $orderby,
                    'show_count'   => $show_count,
                    'pad_counts'   => $pad_counts,
                    'hierarchical' => $hierarchical,
                    'title_li'     => $title,
                    'hide_empty'   => $empty
            );
            $sub_cats = get_categories( $args2 );
            if($sub_cats) {
                foreach($sub_cats as $sub_category) {
                    echo '<li class="subcategory">';
                    echo  '<a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>' ;
                    echo '</li>';
                }   
            } 
        }    
     } }
    else {
         
        
    
              echo '<li>';
              echo '<p>';
              echo 'Currently there is no categories made';
              echo ' or any products in the categories';
              echo '</p>';
              echo '</li>';
       
       } 
    
     echo '</ul>';
     
    ?>
    

  2. Your if statement is in a foreach loop so if there is category it writes the name of that category. When there is no category (or category_parrent isn’t equals to 0 [$cat->category_parent ==0] then) in the last loop it goes into the else statement and tells you that there is category. Just make a if statement before the foreach loop

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search