0

I'm using the query module to display a category, but how to use query to display the child categories??

It is possible??

Thanks,

Stéphane

3

Its not clear from your question whether you're wanting to show the child categories based on the parent category page you're currently viewing. We needed to do the same thing, so I wrote a quick plugin, as I don't think there's any combination of EE tags that will allow you to achieve this. The scenario we used this in was if I was on domain.com/page/category/parent, it would allow me to list out all the children for that parent.

The plugin code looks like this:

class Show_child_cats {

public $return_data;

/**
 * Constructor
 */
public function __construct()
{
    $this->EE =& get_instance();

    // Get information about the current parent and the category group
    $this_cat_parent = $this->EE->TMPL->fetch_param('cat_parent');
    $this_cat_group = $this->EE->TMPL->fetch_param('cat_group');

    // Get the child categories within the group and current parent category set above
    $child_cats = $this->EE->db->query("SELECT exp_categories.cat_id, exp_categories.cat_name, exp_categories.cat_url_title, exp_categories.parent_id, exp_categories.cat_image FROM exp_categories WHERE exp_categories.group_id = '" . $this_cat_group . "' AND exp_categories.parent_id <> 0
AND 
(SELECT exp_categories.cat_id FROM exp_categories WHERE exp_categories.cat_url_title = '" . $this_cat_parent . "' AND exp_categories.group_id = '" . $this_cat_group . "') = exp_categories.parent_id ORDER BY exp_categories.cat_order ASC");

    $vars = array();

    if($child_cats->num_rows() > 0)
    {
        foreach($child_cats->result_array() as $row)
        {
            $vars[] = array(
                'this_cat_name' => $row['cat_name'],
                'this_cat_url_title'  => $row['cat_url_title'],
                'this_cat_id'   => $row['cat_id'],
                'this_cat_parent_id'   => $row['parent_id'],
                'this_cat_image'   => $row['cat_image']
                );
        }
        $this->return_data = $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $vars);
    }
    else
    //not categorised
    {
        $vars[] = array (
            'this_cat_name' => 'There are no child categories for this category.'   
        );
        $this->return_data = $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $vars);
    }
}//end __construct()

The query combines 2 select statements. The first gets all the child categories in the chosen category group (where the parent ID is not 0). The 2nd statement then gets the ID number of the currently viewed category, based on the categories url_title and group_id.

The combined query then lists out the children of the selected parent only.

In my template, I could then call the plugin as follows:

    {exp:show_child_cats cat_parent="{segment_3}" cat_group="1" parse="inward"}
        {if this_cat_url_title!=""}
          <div class="product-cat">
            <a href="/products/category/{this_cat_url_title}" title="{this_cat_name}">
              <h3>{this_cat_name}</h3>
              {if this_cat_image!=""}
                <img src="{exp:parse_file_paths}{this_cat_image}{/exp:parse_file_paths}" alt="{this_cat_name}">
              {if:else}
                <img src="/assets/img/no-image.jpg" alt="{category_name}">
              {/if}
            </a>
          </div>
        {/if}
      {/exp:show_child_cats}

If you need to output a category image the plugin Parse File Paths will convert the path from {filedir_n} to your actual server path.

1
  • Thanks a lot to take time to write a very detailed and complete reply!! It help me a lot! – Stéphane Feb 23 '14 at 12:32

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.