Wednesday, 27 August 2014

How to print the parent category id or slug using its subcategory id or slug in osclass.

I have googled many times for printing parent category "id" or "slug" using subcategoy "id" or "slug" in osclass for some listing functionality.

 In osclass site there is no predefined function to print the parent category "id" or "slug" using subcategory "id" or "slug".

 So i have created one function to print the parent category "id" or "slug" using its subcategory "id" or "slug".

 The function can be added into the functions.php file inside the theme folder. In osclass site we can use custom functions but it should not bother the default functions.

 I have given some explainations with the coding and how to use the function to print the parent category "id" or "slug" using subcategoy "id" or "slug". 

Paste the function at the bottom of the function.php file inside the theme folder. Be sure not to leave any space after the function.

This is the function which accepts only parent id as parameter.so you have to pass the category id as parameter for the given below function when calling the function.
if( !function_exists('bender_item_parentcategory') ){
        function bender_item_parentcategory($category){
            $catDeats = array();
            $aCategory = osc_get_category('id',$category);
            $parentCategory = osc_get_category('id', $aCategory['fk_i_parent_id']);
            $catDeats['current_name'] = $aCategory ['s_name'];
            $catDeats[
current_id'] = $aCategory ['pk_i_id'];
            $catDeats['
current_slug'] = $aCategory ['s_slug'];
            $catDeats[parent_name'] = $parentCategory ['s_name'];
            $catDeats['
parent_id'] = $parentCategory ['pk_i_id'];
            $catDeats['
parent_slug'] = $parentCategory ['s_slug'];
            return $catDeats;       
        }
    } 




This is the function which accepts only parent slug as parameter.so you have to pass the category slug as parameter for the given below function when calling the function.
if( !function_exists('bender_relation_category') ){
        function bender_relation_category
($category){
            $catDeats = array();
            $aCategory = osc_get_category('slug',$category);
          
  $parentCategory = osc_get_category('id', $aCategory['fk_i_parent_id']);
            $catDeats['current_name'] = $aCategory ['s_name'];
            $catDeats[
current_id'] = $aCategory ['pk_i_id'];
            $catDeats['
current_slug'] = $aCategory ['s_slug'];
            $catDeats[parent_name'] = $parentCategory ['s_name'];
            $catDeats['
parent_id'] = $parentCategory ['pk_i_id'];
            $catDeats['
parent_slug'] = $parentCategory ['s_slug'];
            return $catDeats;        
        }
    }



Call the function anywhere in the theme files and pass the category id as parameter for the function .
 you will get a result array which has the details for slugname, parent category id ect...

Function calling example using category id as parameter,

$values = bender_item_parentcategory(12);


Function calling example using category slug as parameter,

$values = bender_relation_category('userd-cars');


you can print the result array using print_r($values) for your understanding.

If the parameter your passing to the function is the parent category , then the result array only print its category name, slug, category id in the result array.

If the parameter your passing to the function is subcategory , then you will get the category and its parent category details.