fix(widgets): resolve term covers through the canonical category chain #24

Merged
kate522 merged 1 commits from worktree/fix-term-aggregation-cover into main 2026-09-05 04:07:40 +00:00
+27 -7
View File
@@ -40,16 +40,36 @@ endif;
if (!function_exists('aksrt_widget_term_cover_url')) : if (!function_exists('aksrt_widget_term_cover_url')) :
/** /**
* Category cover image URL with the theme meta fallback chain. * Category cover image URL, resolved through the theme's canonical chain:
* explicit term cover → latest thumbnailed post inside the term → optional
* default placeholder. The reference hides term cards without a real cover
* while the aggregation card always falls back to a default image, so the
* last step is opt-in via $default_fallback.
*/ */
function aksrt_widget_term_cover_url(int $term_id): string { function aksrt_widget_term_cover_url(int $term_id, bool $default_fallback = false): string {
foreach (['aksrt_cover_url', 'cover_url'] as $meta_key) { $cover_id = (int) get_term_meta($term_id, 'aksrt_category_cover_id', true);
$url = esc_url_raw((string) get_term_meta($term_id, $meta_key, true)); if ($cover_id > 0) {
$url = wp_get_attachment_url($cover_id);
if ($url !== '') { if ($url !== '') {
return $url; return (string) $url;
} }
} }
return ''; $posts = get_posts([
'category__in' => [$term_id],
'posts_per_page' => 1,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => [['key' => '_thumbnail_id', 'compare' => 'EXISTS']],
'fields' => 'ids',
]);
if (!empty($posts)) {
$url = get_the_post_thumbnail_url((int) $posts[0], 'medium_large');
if ($url) {
return (string) $url;
}
}
return $default_fallback ? get_template_directory_uri() . '/assets/img/placeholder.svg' : '';
} }
endif; endif;
@@ -195,7 +215,7 @@ if (!function_exists('aksrt_widget_term_aggregation')) :
$href = esc_url((string) $href); $href = esc_url((string) $href);
$icon = '<i class="fa fa-folder-open-o mr6" aria-hidden="true"></i>'; $icon = '<i class="fa fa-folder-open-o mr6" aria-hidden="true"></i>';
$img = aksrt_widget_term_cover_url((int) $term->term_id); $img = aksrt_widget_term_cover_url((int) $term->term_id, true);
$img_html = aksrt_graphic_card([ $img_html = aksrt_graphic_card([
'class' => '', 'class' => '',
'img' => $img, 'img' => $img,