feat: Added an option to have titles displayed under cards
This commit is contained in:
@@ -382,6 +382,20 @@ class Elementor_Category_Grid_Widget extends \Elementor\Widget_Base
|
||||
]
|
||||
);
|
||||
|
||||
$this->add_control(
|
||||
'title_position',
|
||||
[
|
||||
'label' => esc_html__('Position du titre', 'category-grid-widget-for-elementor'),
|
||||
'type' => Controls_Manager::SELECT,
|
||||
'options' => [
|
||||
'overlay' => esc_html__('Sur la carte', 'category-grid-widget-for-elementor'),
|
||||
'below' => esc_html__('Sous la carte', 'category-grid-widget-for-elementor'),
|
||||
],
|
||||
'default' => 'overlay',
|
||||
'description' => esc_html__('Choisissez si le nom de la catégorie apparaît en overlay ou sous l’image.', 'category-grid-widget-for-elementor'),
|
||||
]
|
||||
);
|
||||
|
||||
$this->end_controls_section();
|
||||
|
||||
/* Section "Mise en page" pour les options d'affichage (colonnes, taille d'image) */
|
||||
@@ -439,108 +453,91 @@ class Elementor_Category_Grid_Widget extends \Elementor\Widget_Base
|
||||
{
|
||||
$settings = $this->get_settings_for_display();
|
||||
|
||||
// Récupérer les ID des catégories sélectionnées.
|
||||
// 1) Récupérer les IDs des catégories sélectionnées
|
||||
$selected_ids = [];
|
||||
if (!empty($settings['categories'])) {
|
||||
// S'assurer d'avoir un tableau d'ID (entiers).
|
||||
$selected_ids = is_array($settings['categories']) ? $settings['categories'] : [$settings['categories']];
|
||||
$selected_ids = array_map('intval', $selected_ids);
|
||||
$selected_ids = (array) $settings['categories'];
|
||||
$selected_ids = array_map('absint', $selected_ids);
|
||||
}
|
||||
if (empty($selected_ids)) {
|
||||
// Aucune catégorie sélectionnée : ne rien afficher.
|
||||
return;
|
||||
}
|
||||
|
||||
// Déterminer les catégories à afficher (inclure les sous-catégories si demandé).
|
||||
$category_ids_to_show = $selected_ids;
|
||||
|
||||
// 1) Cas : on veut les sous-catégories de la page actuelle
|
||||
// 2) Construire la liste des IDs à afficher
|
||||
if ('yes' === $settings['subcats_of_current']) {
|
||||
$queried = get_queried_object();
|
||||
if ($queried && isset($queried->term_id) && 'category' === $queried->taxonomy) {
|
||||
if ($queried instanceof WP_Term && '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 = [];
|
||||
$subs = [];
|
||||
foreach ($selected_ids as $cat_id) {
|
||||
$children = get_term_children($cat_id, 'category');
|
||||
if (is_array($children)) {
|
||||
$sub_ids = array_merge($sub_ids, $children);
|
||||
$subs = array_merge($subs, $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']) {
|
||||
$category_ids_to_show = array_unique($subs);
|
||||
} 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') ?: []);
|
||||
$children = get_term_children($cat_id, 'category') ?: [];
|
||||
$category_ids_to_show = array_merge($category_ids_to_show, $children);
|
||||
}
|
||||
$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
|
||||
// 3) Préparer args pour get_terms()
|
||||
$term_args = [
|
||||
'taxonomy' => 'category',
|
||||
'include' => $category_ids_to_show,
|
||||
'hide_empty' => false,
|
||||
];
|
||||
if ('count' !== $settings['order_by']) {
|
||||
// tri natif pour 'name' ou 'id'
|
||||
$term_args['orderby'] = $settings['order_by'];
|
||||
$term_args['order'] = $settings['order'];
|
||||
}
|
||||
|
||||
// Récupérer les termes
|
||||
// 4) Récupérer et filtrer les termes
|
||||
$raw_terms = get_terms($term_args);
|
||||
if (is_wp_error($raw_terms) || empty($raw_terms)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Filtrer les termes sans image si demandé
|
||||
$terms = [];
|
||||
foreach ($raw_terms as $term) {
|
||||
$thumb_id = get_term_meta($term->term_id, 'thumbnail_id', true);
|
||||
$image_url = $thumb_id
|
||||
? wp_get_attachment_image_url($thumb_id, $settings['image_size'])
|
||||
: '';
|
||||
|
||||
$image_url = $thumb_id ? wp_get_attachment_image_url($thumb_id, $settings['image_size']) : '';
|
||||
if ('yes' === $settings['hide_without_image'] && empty($image_url)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Stocker image_url temporaire pour éviter de recalculer
|
||||
$term->_image_url = $image_url;
|
||||
$terms[] = $term;
|
||||
}
|
||||
if (empty($terms)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Si tri par nombre d'articles, calculer et trier manuellement
|
||||
// 5) Tri manuel si order_by=count
|
||||
if ('count' === $settings['order_by']) {
|
||||
// Calcul du total d'articles (directs + descendants) pour chaque terme
|
||||
$counts = [];
|
||||
foreach ($terms as $term) {
|
||||
$total = (int) $term->count;
|
||||
$desc_ids = get_term_children($term->term_id, 'category');
|
||||
if (is_array($desc_ids)) {
|
||||
foreach ($desc_ids as $desc_id) {
|
||||
$desc = get_term($desc_id, 'category');
|
||||
if (!is_wp_error($desc)) {
|
||||
$total += (int) $desc->count;
|
||||
$total = intval($term->count);
|
||||
$children = get_term_children($term->term_id, 'category');
|
||||
if (is_array($children)) {
|
||||
foreach ($children as $child_id) {
|
||||
$child = get_term($child_id, 'category');
|
||||
if (!is_wp_error($child)) {
|
||||
$total += intval($child->count);
|
||||
}
|
||||
}
|
||||
}
|
||||
$counts[$term->term_id] = $total;
|
||||
}
|
||||
// Tri selon l’ordre choisi
|
||||
usort($terms, function ($a, $b) use ($counts, $settings) {
|
||||
$ca = $counts[$a->term_id];
|
||||
$cb = $counts[$b->term_id];
|
||||
@@ -548,40 +545,58 @@ class Elementor_Category_Grid_Widget extends \Elementor\Widget_Base
|
||||
return 0;
|
||||
}
|
||||
if ('ASC' === $settings['order']) {
|
||||
return ($ca < $cb) ? -1 : 1;
|
||||
return $ca < $cb ? -1 : 1;
|
||||
}
|
||||
return ($ca > $cb) ? -1 : 1;
|
||||
return $ca > $cb ? -1 : 1;
|
||||
});
|
||||
}
|
||||
|
||||
// Calcul de la classe grille
|
||||
$columns = (int) $settings['columns'];
|
||||
$grid_class = 'columns-' . $columns;
|
||||
// 6) Affichage final
|
||||
$grid_class = 'columns-' . absint($settings['columns']);
|
||||
$is_below = ('below' === $settings['title_position']);
|
||||
|
||||
// Rendu HTML
|
||||
echo '<div class="elementor-category-grid ' . esc_attr($grid_class) . '">';
|
||||
|
||||
foreach ($terms as $term) {
|
||||
$thumbnail_id = get_term_meta($term->term_id, 'thumbnail_id', true);
|
||||
echo '<div class="category-card">';
|
||||
echo '<a href="' . esc_url(get_term_link($term)) . '">';
|
||||
if (esc_url($term->_image_url)) {
|
||||
$thumb_id = get_term_meta($term->term_id, 'thumbnail_id', true);
|
||||
$link = get_term_link($term);
|
||||
if (is_wp_error($link)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Ouvre la carte et ajoute la classe si titre en dessous
|
||||
$card_class = 'category-card' . ($is_below ? ' title-below' : '');
|
||||
echo '<div class="' . esc_attr($card_class) . '">';
|
||||
|
||||
// Image cliquable
|
||||
echo '<a href="' . esc_url($link) . '">';
|
||||
if ($thumb_id) {
|
||||
echo '<div class="category-card-image">';
|
||||
echo wp_get_attachment_image(
|
||||
$thumbnail_id,
|
||||
$thumb_id,
|
||||
$settings['image_size'],
|
||||
false,
|
||||
[
|
||||
'alt' => esc_attr($term->name),
|
||||
'class' => 'category-card-img', // optionnel : ajoutez vos classes CSS
|
||||
'class' => 'category-card-img',
|
||||
]
|
||||
);
|
||||
echo '</div>';
|
||||
}
|
||||
echo '<div class="category-card-name">' . esc_html($term->name) . '</div>';
|
||||
echo '</a>';
|
||||
echo '</div>';
|
||||
}
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
// Titre (overlay ou below) **à l’intérieur** de la carte
|
||||
if ($is_below) {
|
||||
echo '<div class="category-card-name-below">';
|
||||
echo '<a href="' . esc_url($link) . '">' . esc_html($term->name) . '</a>';
|
||||
echo '</div>';
|
||||
} else {
|
||||
echo '<div class="category-card-name overlay">' . esc_html($term->name) . '</div>';
|
||||
}
|
||||
|
||||
echo '</div>'; // .category-card
|
||||
}
|
||||
|
||||
echo '</div>'; // .elementor-category-grid
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user