feat: Added more option to handle subcategories

This commit is contained in:
MrRaph_
2025-05-23 14:14:50 +00:00
parent e89e4ad7c2
commit 1704d64ed5
3 changed files with 77 additions and 14 deletions

View File

@@ -3,7 +3,7 @@
* Plugin Name: Category Grid Widget for Elementor * Plugin Name: Category Grid Widget for Elementor
* Plugin URI: https://git.mrraph.fr/WordPress/elementor-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. * Description: Responsive article category grid with image for Elementor.
* Version: 1.2.1 * Version: 1.3.0
* Author: MrRaph_ * Author: MrRaph_
* Author URI: https://mrraph.photo * Author URI: https://mrraph.photo
* Requires at least: 5.8 * Requires at least: 5.8
@@ -45,7 +45,7 @@ function ccgw_enqueue_media_uploader($hook_suffix)
'ccgw-category-image', 'ccgw-category-image',
plugin_dir_url(__FILE__) . 'admin/js/category-image.js', plugin_dir_url(__FILE__) . 'admin/js/category-image.js',
['jquery'], ['jquery'],
'1.2.1', '1.3.0',
true true
); );
@@ -223,7 +223,7 @@ function ccgw_enqueue_front_styles()
'ccgw-category-grid-style', 'ccgw-category-grid-style',
plugin_dir_url(__FILE__) . 'css/style.css', plugin_dir_url(__FILE__) . 'css/style.css',
[], [],
'1.2.1' '1.3.0'
); );
} }
} }

View File

@@ -5,7 +5,7 @@ Tags: elementor, category, grid, widget, posts
Requires at least: 5.8 Requires at least: 5.8
Tested up to: 6.8 Tested up to: 6.8
Requires PHP: 7.0 Requires PHP: 7.0
Stable tag: 1.2.1 Stable tag: 1.3.0
License: GPLv2 or later License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html License URI: https://www.gnu.org/licenses/gpl-2.0.html
Responsive grid of post categories with images for Elementor. Responsive grid of post categories with images for Elementor.
@@ -46,6 +46,9 @@ Yes, enable the **“Hide categories without image”** option in the widgets
Open the **Style » Hover** tab: choose the overlay color & opacity, and the title color on hover. Open the **Style » Hover** tab: choose the overlay color & opacity, and the title color on hover.
== Changelog == == Changelog ==
= 1.3.0 =
* Added more option to handle subcategories
= 1.2.1 = = 1.2.1 =
* Initial release * Initial release
* Manual category and subcategory selection * Manual category and subcategory selection

View File

@@ -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 lon est sur une page darchive de catégorie, affichera ses sous-catégories.', 'category-grid-widget-for-elementor'),
]
);
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// Contrôle : Ne montrer que les catégories avec image // Contrôle : Ne montrer que les catégories avec image
$this->add_control( $this->add_control(
@@ -426,17 +453,50 @@ class Elementor_Category_Grid_Widget extends \Elementor\Widget_Base
// Déterminer les catégories à afficher (inclure les sous-catégories si demandé). // Déterminer les catégories à afficher (inclure les sous-catégories si demandé).
$category_ids_to_show = $selected_ids; $category_ids_to_show = $selected_ids;
if (!empty($settings['show_subcategories']) && $settings['show_subcategories'] === 'yes') { // if (!empty($settings['show_subcategories']) && $settings['show_subcategories'] === 'yes') {
// Ajouter les sous-catégories de chaque catégorie sélectionnée. // // 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) { foreach ($selected_ids as $cat_id) {
$child_ids = get_term_children($cat_id, 'category'); $children = get_term_children($cat_id, 'category');
if (is_array($child_ids) && !empty($child_ids)) { if (is_array($children)) {
$category_ids_to_show = array_merge($category_ids_to_show, $child_ids); $sub_ids = array_merge($sub_ids, $children);
} }
} }
// Éliminer les doublons d'ID au cas où. $category_ids_to_show = array_unique($sub_ids);
}
// Sinon, lancienne 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); $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 // Préparer les args pour get_terms() on ne précise PAS le tri si c'est par count
$term_args = [ $term_args = [