subcategory display in serach page osclass
output
function.php
Add this code into functions.php to get subcategory list based on category selection and display into search.php
if( !function_exists('get_subcategories') ) {
function get_subcategories( ) {
$location = Rewrite::newInstance()->get_location() ;
$section = Rewrite::newInstance()->get_section() ;
if ( $location != 'search' ) {
return false ;
}
$category_id = osc_search_category_id() ;
if(count($category_id) > 1) {
return false ;
}
$category_id = (int) $category_id[0] ;
$subCategories = Category::newInstance()->findSubcategories($category_id) ;
foreach($subCategories as &$category) {
$category['url'] = get_category_url($category) ;
}
return $subCategories ;
}
}
if ( !function_exists('get_category_url') ) {
function get_category_url( $category ) {
$path = '';
if ( osc_rewrite_enabled() ) {
if ($category != '') {
$category = Category::newInstance()->hierarchy($category['pk_i_id']) ;
$sanitized_category = "" ;
for ($i = count($category); $i > 0; $i--) {
$sanitized_category .= $category[$i - 1]['s_slug'] . '/' ;
}
$path = osc_base_url() . $sanitized_category ;
}
} else {
$path = sprintf( osc_base_url(true) . '?page=search&sCategory=%d', $category['pk_i_id'] ) ;
}
return $path;
}
}
if ( !function_exists('get_category_num_items') ) {
function get_category_num_items( $category ) {
$category_stats = CategoryStats::newInstance()->countItemsFromCategory($category['pk_i_id']) ;
if( empty($category_stats) ) {
return 0 ;
}
return $category_stats;
}
}
?>
//search .php
Add this code into search.php where you want to display your sub category
<ul>
<li><?php foreach(get_subcategories() as $subcat) { echo "<a href='".$subcat["url"]."'>".$subcat["s_name"]."</a> <span>(".get_category_num_items($subcat).")</span> | " ; } ?>
</li>
</ul>
0 comments:
Post a Comment
Thanks for your valuable feedback