Drupal 6 - 从特定类别和子类别中获取节点

I am working in one site which is developed in drupal 6. The top menu contains categories & clicking on filters the nodes that falls on that on that category as well as sub category.

On the right side, there is a block named "Sustainability Services" which displays the nodes randomly.

Here is my current code which I am having right now. Also I have added the image of the site.enter image description here

    function node_load_by_type_right_block($type) 
    {
      $node = menu_get_object();
      $taxonomy = $node->taxonomy;  
      $nodes = array();
      $query= '';

      if($taxonomy) 
      { 
        $temp = array();
        foreach($taxonomy as $t) { 
           $temp[] = $t->tid;
        }

        $taxo = implode(",", $temp);

         $query = "SELECT n.nid FROM {node} n LEFT JOIN {term_node} tn ON tn.vid = n.vid WHERE type = 'service_provider' AND status = 1 AND tn.tid in($taxo) ORDER BY nid DESC limit 3 ";
         $results = db_query($query);  
         $count =  mysql_num_rows($results);
         //echo $count; 

         while($nid = db_result($results)) {  
             if($nid >= 530)
             $main[] = node_load($nid);   

      }  

      return $main;
    }

The above code display the random nodes from various categories.

What I want to do is when I click on any menu item (which are rendered as categories), then the Sustaniblity Services block should filter based on that category & sub category. If there are no more nodes in that category then random nodes should be displayed. As I am new to drupal, I am having a hard time dealing with it.

One of the simpler way to achieve this would be to use Views & Panels

With Panels, you should be able to recreate a page layout composed of various Views that can react to a number of Contextual Filters.

There are a lot of online tutorials about Views & Panels, it should be fairly easy to find an example of what you want to achieve.