WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] List all categories and subcategories in a nested list (4 posts)

  1. alex_marin_29
    Member
    Posted 1 year ago #

    I am trying to list all categories and subcategories in a nested list like this, but only if exist at least a subcategory post. If no subcategory of one category has no posts, the category will not be displayed.

    <ul>
    <li>category1</li>
    </ul>
    <ul>
    <li>subcateg1</li>
    <li>subcateg2</li>
    <li>subcateg3</li>
    </ul>
    <li>category2</li>
    <ul>
    <li>subcateg1</li>
    <li>subcateg2</li>
    </ul>

    Here is my code:

    $category_ids = get_all_category_ids();
      $parent_categories = array();
        foreach($category_ids as $cat_id) {
            $category = get_category($cat_id);
            if ($category->parent == 0) {
               $parent_categories[] = $category->cat_ID;
            }
        }
        foreach ($parent_categories as $pcat) :
            $category = get_category($pcat);
            if ($category->taxonomy == 'category' ) :
    ?>
        <ul class="subcategory_title" >
    
    <li>- <?php echo ucfirst($category->name); ?></li>
    <ul>
    	<?php
                 $childArgs =    array(
                 'title_li'  => '',
                 'child_of'  =>$category->cat_ID
                 );
             $categories=  get_categories($childArgs);
             foreach ($categories as $categoryy) {
    	$option = '
    <li>- '.$categoryy->cat_name.'</li>
    ';
    	echo $option;
    }
    	?>
             </ul>

    [Please use the code buttons - as is, your code may have been damaged and it messed up the forum - now fixed]

  2. alex_marin_29
    Member
    Posted 1 year ago #

    The problem with this code is that it display the categories if his subcategories has no post.

  3. lazaroznmix
    Member
    Posted 4 months ago #

    I did this code and it works perfectly!

    <?php
    /**
    *Display categories list
    Lázaro Rodrigues
    http://www.znmix.com.br
    */
    $query_category = mysql_query("SELECT wp_terms.term_id,wp_terms.name, wp_term_taxonomy.parent FROM wp_terms INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id WHERE wp_term_taxonomy.taxonomy = 'product_cat' ORDER BY name");
    		$data = array();
    		$index = array();
    		while ($row = mysql_fetch_assoc($query_category)) {
    		    $id = $row["term_id"];
    		    $parent_id = $row["parent"] === NULL ? "NULL" : $row["parent"];
    		    $data[$id] = $row;
    		    $index[$parent_id][] = $id;
    		}
    		function display_child_nodes($parent_id, $level){
    		    global $data, $index;
    		    $parent_id = $parent_id === NULL ? "NULL" : $parent_id;
    		    if (isset($index[$parent_id])) {
    		    	echo "
    
    <ul>";
    		        foreach ($index[$parent_id] as $id) {
    		        	if($level == 0){
    						echo "
    
    <li><h3><strong>".$data[$id]["name"]."</strong>";
    		        	}
    		        	if($level == 1){
    						echo "</li>
    <li>".$data[$id]["name"]."</li>
    ";
    		           	}
    		           //echo str_repeat("-", $level) . $data[$id]["name"] . "
    
    <li>";
    		           display_child_nodes($id, $level + 1);
    	        		if($level != 1){
    						echo "</li>
    ";
    	        		}
    		        }
    		        echo "</ul>
    ";
    		    }
    		}
    		display_child_nodes(0, 0);
    ?>

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum's parser.]

  4. gautamarya
    Member
    Posted 4 months ago #

    Hi lazaroznmix,

    Great help, I was looking for this solutions since long time.

    However I've changed this code in function and changed some code also to suite my requirement.

    I'm posting it here for future reference and/or for somebody like me.
    Hope It helps.

    function getChildCategories($taxo_type){
    
    	$query_category = mysql_query("SELECT wp_terms.term_id,wp_terms.name, wp_term_taxonomy.parent FROM wp_terms INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id WHERE wp_term_taxonomy.taxonomy = '".$taxo_type."' ORDER BY name");
    		$data = array();
    		$index = array();
    		while ($row = mysql_fetch_assoc($query_category)) {
    			global $index, $data;
    		    $id = $row["term_id"];
    		    $parent_id = $row["parent"] === NULL ? "NULL" : $row["parent"];
    		    $data[$id] = $row;
    		    $index[$parent_id][] = $id;
    		}                
    
    		function display_child_nodes($parent_id, $level){
    		    global $data, $index;
    		    $parent_id = $parent_id === NULL ? "NULL" : $parent_id;
    		    if (isset($index[$parent_id])) {
    		    	if($level == 0){ echo "<ul>"; }
    		        foreach ($index[$parent_id] as $id) {
    		        	if($level == 0){
    						echo "<li><h3>".$data[$id]["name"]."</h3>";
    		        	}
    		        	if($level >= 1){
    						//echo "<li>".$data[$id]["name"]."</li>";
    						echo "<li>".$data[$id]["name"];
    		           	}
    		           //echo str_repeat("-", $level) . $data[$id]["name"] . ";
    
    					echo "<ul>";
    		           		display_child_nodes($id, $level + 1);
    	        		if($level != 1){
    					echo "</ul>";
    	        		}
    		        }
    		        if($level == 0){ echo "</ul>"; }
    		    }
    		}
    		display_child_nodes(0, 0);
    }

    Basically I was trying achieve HTML structure like

    <ul>
    	<li>
        	<h3> Network Cam. </h3>
            <ul>
            	<li>AM Seriees
                	<ul>
                        <li>1080P 2MP IP Camera
                        	<ul>
                                <li>BOX Camera</li>
                                <li>Dome Camera</li>
                                <li>PTZ Camera</li>
                                <li>Waterproof Camera</li>
                            </ul>
                        </li>
                        <li>960P 1.3 MP IP Camera
                        	<ul>
                                <li>BOX Camera</li>
                                <li>Dome Camera</li>
                                <li>PTZ Camera</li>
                                <li>Waterproof Camera</li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li>TI Seriees
                	<ul>
                        <li>1080P 2MP IP Camera
                        	<ul>
                                <li>BOX Camera</li>
                                <li>Dome Camera</li>
                                <li>PTZ Camera</li>
                                <li>Waterproof Camera</li>
                            </ul>
                        </li>
                        <li>960P 1.3 MP IP Camera
                        	<ul>
                                <li>BOX Camera</li>
                                <li>Dome Camera</li>
                                <li>PTZ Camera</li>
                                <li>Waterproof Camera</li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>

    but it show <ul></ul> after the last child of the category. But for now the devil is gone. :)
    Kudos to @lazaroznmix

    Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic