diff --git a/category-grid-widget-for-elementor.php b/category-grid-widget-for-elementor.php index 392038e..89ce20d 100644 --- a/category-grid-widget-for-elementor.php +++ b/category-grid-widget-for-elementor.php @@ -3,7 +3,7 @@ * Plugin Name: Category Grid Widget for Elementor * Plugin URI: https://git.mrraph.fr/WordPress/elementor-category-grid-widget-for-elementor * Description: Responsive article category grid with image for Elementor. - * Version: 1.2.1 + * Version: 1.3.0 * Author: MrRaph_ * Author URI: https://mrraph.photo * Requires at least: 5.8 @@ -45,7 +45,7 @@ function ccgw_enqueue_media_uploader($hook_suffix) 'ccgw-category-image', plugin_dir_url(__FILE__) . 'admin/js/category-image.js', ['jquery'], - '1.2.1', + '1.3.0', true ); @@ -223,7 +223,7 @@ function ccgw_enqueue_front_styles() 'ccgw-category-grid-style', plugin_dir_url(__FILE__) . 'css/style.css', [], - '1.2.1' + '1.3.0' ); } } diff --git a/readme.txt b/readme.txt index e39e9bd..dd6f848 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Tags: elementor, category, grid, widget, posts Requires at least: 5.8 Tested up to: 6.8 Requires PHP: 7.0 -Stable tag: 1.2.1 +Stable tag: 1.3.0 License: GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html Responsive grid of post categories with images for Elementor. @@ -46,6 +46,9 @@ Yes, enable the **“Hide categories without image”** option in the widget’s Open the **Style » Hover** tab: choose the overlay color & opacity, and the title color on hover. == Changelog == += 1.3.0 = +* Added more option to handle subcategories + = 1.2.1 = * Initial release * Manual category and subcategory selection diff --git a/widgets/category-grid-widget.php b/widgets/category-grid-widget.php index e254650..f46f056 100644 --- a/widgets/category-grid-widget.php +++ b/widgets/category-grid-widget.php @@ -311,6 +311,33 @@ class Elementor_Category_Grid_Widget extends \Elementor\Widget_Base ] ); + $this->add_control( + 'only_subcats_of_selected', + [ + 'label' => esc_html__('Afficher seulement les sous-catégories des catégories sélectionnées', 'category-grid-widget-for-elementor'), + 'type' => \Elementor\Controls_Manager::SWITCHER, + 'label_on' => esc_html__('Oui', 'category-grid-widget-for-elementor'), + 'label_off' => esc_html__('Non', 'category-grid-widget-for-elementor'), + 'return_value' => 'yes', + 'default' => 'no', + 'description' => esc_html__('Si activé, seules les sous-catégories des catégories choisies seront affichées (et pas les catégories parentes).', 'category-grid-widget-for-elementor'), + ] + ); + + // Contrôle : Afficher les sous-catégories de la page catégorie actuelle + $this->add_control( + 'subcats_of_current', + [ + 'label' => esc_html__('Afficher les sous-catégories de la catégorie actuelle', 'category-grid-widget-for-elementor'), + 'type' => \Elementor\Controls_Manager::SWITCHER, + 'label_on' => esc_html__('Oui', 'category-grid-widget-for-elementor'), + 'label_off' => esc_html__('Non', 'category-grid-widget-for-elementor'), + 'return_value' => 'yes', + 'default' => 'no', + 'description' => esc_html__('Si activé et que l’on est sur une page d’archive de catégorie, affichera ses sous-catégories.', 'category-grid-widget-for-elementor'), + ] + ); + // ---------------------------------------------------------------------- // Contrôle : Ne montrer que les catégories avec image $this->add_control( @@ -426,16 +453,49 @@ class Elementor_Category_Grid_Widget extends \Elementor\Widget_Base // Déterminer les catégories à afficher (inclure les sous-catégories si demandé). $category_ids_to_show = $selected_ids; - if (!empty($settings['show_subcategories']) && $settings['show_subcategories'] === 'yes') { - // Ajouter les sous-catégories de chaque catégorie sélectionnée. - foreach ($selected_ids as $cat_id) { - $child_ids = get_term_children($cat_id, 'category'); - if (is_array($child_ids) && !empty($child_ids)) { - $category_ids_to_show = array_merge($category_ids_to_show, $child_ids); - } + // if (!empty($settings['show_subcategories']) && $settings['show_subcategories'] === 'yes') { + // // Ajouter les sous-catégories de chaque catégorie sélectionnée. + // foreach ($selected_ids as $cat_id) { + // $child_ids = get_term_children($cat_id, 'category'); + // if (is_array($child_ids) && !empty($child_ids)) { + // $category_ids_to_show = array_merge($category_ids_to_show, $child_ids); + // } + // } + // // Éliminer les doublons d'ID au cas où. + // $category_ids_to_show = array_unique($category_ids_to_show); + // } + // 1) Cas : on veut les sous-catégories de la page actuelle + if ('yes' === $settings['subcats_of_current']) { + $queried = get_queried_object(); + if ($queried && isset($queried->term_id) && 'category' === $queried->taxonomy) { + $category_ids_to_show = get_term_children($queried->term_id, 'category'); + } else { + // si pas en archive catégorie, rien à afficher + return; + } + + // 2) Sinon, on part des catégories sélectionnées + } else { + $category_ids_to_show = $selected_ids; + + // Si on veut uniquement leurs sous-catégories + if ('yes' === $settings['only_subcats_of_selected']) { + $sub_ids = []; + foreach ($selected_ids as $cat_id) { + $children = get_term_children($cat_id, 'category'); + if (is_array($children)) { + $sub_ids = array_merge($sub_ids, $children); + } + } + $category_ids_to_show = array_unique($sub_ids); + } + // Sinon, l’ancienne logique : on ajoute ou pas les sous-catégories selon show_subcategories + elseif ('yes' === $settings['show_subcategories']) { + foreach ($selected_ids as $cat_id) { + $category_ids_to_show = array_merge($category_ids_to_show, get_term_children($cat_id, 'category') ?: []); + } + $category_ids_to_show = array_unique($category_ids_to_show); } - // Éliminer les doublons d'ID au cas où. - $category_ids_to_show = array_unique($category_ids_to_show); } // Préparer les args pour get_terms() – on ne précise PAS le tri si c'est par count @@ -511,7 +571,7 @@ class Elementor_Category_Grid_Widget extends \Elementor\Widget_Base // Rendu HTML echo '
'; foreach ($terms as $term) { - $thumbnail_id = get_term_meta( $term->term_id, 'thumbnail_id', true ); + $thumbnail_id = get_term_meta($term->term_id, 'thumbnail_id', true); echo '
'; echo ''; if (esc_url($term->_image_url)) {