1

I wanted to share this work with you since I did not find any solution on the internet, so I had to build mine.

The problem was that the client wanted to show the subcategories within the main category using a shortcode. Only the name of the subcategories, without thumbnail or number of products.

General Grievance
  • 4,555
  • 31
  • 31
  • 45

1 Answers1

0

So create a file called "products-subcategory.php" in plugins/bezel-addons/vc/shortcodes/. And I incorporated it in plugins/bezel-addons/vc/shortcodes.php In my case my template is the Bezzel https://themeforest.net/item/bezel-creative-multipurpose-wordpress-theme/20014332 But I think you can implement it in anyone using Visual Composer or WP Bakery.


products-subcategory.php.

<?php
/* Product subcategory  */
vc_map(
  array(
    'name' => 'Product subcategory',
    'base' => 'bezel_products_subcategory',
    'icon' => 'ti-align-left',
    'description' => 'Product subcategories',
    'category' => __( 'Bezel', 'bezel-addons'),
    'params' => array(
      array(
        'type' => 'dropdown',
        'param_name' => 'orderby',
        'heading' => 'Order BY',
        'value' => array(
          'Name' => 'name',
          'ID' => 'term_id'
        ),
      ),
    array(
        'type' => 'dropdown',
        'param_name' => 'order',
        'heading' => 'Order',
        'value' => array(
            'Upward' => 'ASC',
            'Falling' => 'DESC'
        ),
      ),
        array(
        'type' => 'dropdown',
        'param_name' => 'empty',
        'heading' => 'Show empty subcategories',
        'value' => array(
            'Yes' => 0,
            'No' => 1
        ),
      )
     
    )
  )
);

add_shortcode( 'bezel_products_subcategory', 'bezel_products_subcategory' );

function bezel_products_subcategory( $atts ) {
 global $wp_query;

    extract( shortcode_atts( array(
        'taxonomy' => 'product_cat',
        'orderby' => 'name',
        'order' => 'ASC',
        'empty' => 0,
        'hierarchical' => 1
  ), $atts ) );

    $cat = get_queried_object();        
    $category_id = ($cat->parent) ? $cat->parent : $cat->term_id;
    $args2 = array('taxonomy' => $taxonomy,'parent' => $category_id,'hierarchical' => $hierarchical, 'orderby' => $orderby, 'order' => $order,'hide_empty' => $empty);
    $categories = get_categories( $args2 );
    $categories_cnt = count(get_categories( $args2 ));

    $selcat[$cat->term_id] = 'current-cat';
    
    if ($categories_cnt != 0){

        $sub_cats = get_categories( $args2 );
        if($sub_cats) {
            $output = '<div class="vc_wp_categories wpb_content_element">';
            $output .= '<div class="widget widget_categories">';
            $output .= '<ul>';
            foreach($sub_cats as $sub_category) {
                $output .= '<li class="cat-item cat-item-'.$sub_category->term_id.' '.$selcat[$sub_category->term_id].'"><a href="'.get_category_link($sub_category->term_id).'">'.$sub_category->cat_name.'</a></li>';
            }
            $output .= '</ul>';
            $output .= '</div>';
            $output .= '</div>';
        }
    }

  return $output;
}
?>
General Grievance
  • 4,555
  • 31
  • 31
  • 45