The problem with this code is that it display the categories if his subcategories has no post.
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.]
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.