From 15b4c818c50f48912c590bae447ddc11045eefc8 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 14 Aug 2026 14:12:17 +0800 Subject: [PATCH] fix: collapse empty homepage sidebar --- inc/helpers/aksrt-template-tags.php | 31 ++++++++++++++++++++++++++--- inc/helpers/homepage/content.php | 17 +++++++++++----- index.php | 11 ++++++---- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/inc/helpers/aksrt-template-tags.php b/inc/helpers/aksrt-template-tags.php index 96796a82..baec2578 100644 --- a/inc/helpers/aksrt-template-tags.php +++ b/inc/helpers/aksrt-template-tags.php @@ -54,6 +54,29 @@ function aksrt_resolve_sidebar_id(): string { } endif; +if (!function_exists('aksrt_has_current_sidebar_widgets')): +/** + * Whether the current page has widgets for its site-sidebar column. + * + * The global top/bottom zones are rendered alongside the page-specific + * sidebar, so any of these zones makes the column meaningful. + */ +function aksrt_has_current_sidebar_widgets(): bool { + $sidebar_id = function_exists('aksrt_resolve_sidebar_id') + ? aksrt_resolve_sidebar_id() + : 'home_sidebar'; + $sidebar_ids = array_unique(['all_sidebar_top', $sidebar_id, 'all_sidebar_bottom']); + + foreach ($sidebar_ids as $sidebar_id) { + if (is_active_sidebar($sidebar_id)) { + return true; + } + } + + return false; +} +endif; + if (!function_exists('aksrt_get_post_show_layout')): /** * Zibll-compatible post/page layout override. @@ -218,9 +241,7 @@ function aksrt_get_site_layout(): int { if (is_single()) { $post_id = (int) get_queried_object_id(); $layout = aksrt_get_post_show_layout($post_id); - $has_article_widgets = is_active_sidebar('all_sidebar_top') - || is_active_sidebar('sidebar-article') - || is_active_sidebar('all_sidebar_bottom'); + $has_article_widgets = aksrt_has_current_sidebar_widgets(); // Keep the sidebar in the document whenever it is configured. CSS handles // narrow viewports; wp_is_mobile() is User-Agent based and can otherwise // suppress widgets on a desktop-sized page. @@ -243,6 +264,10 @@ function aksrt_get_site_layout(): int { return 1; } + if (!aksrt_has_current_sidebar_widgets()) { + return 1; + } + $position = 'right'; if (is_singular()) { $meta = aksrt_get_post_show_layout(get_queried_object_id()); diff --git a/inc/helpers/homepage/content.php b/inc/helpers/homepage/content.php index 9c11bcdc..b197a710 100644 --- a/inc/helpers/homepage/content.php +++ b/inc/helpers/homepage/content.php @@ -146,14 +146,21 @@ if (!function_exists('aksrt_homepage_has_sidebar')) { * Whether the fixed homepage should render an aside column. */ function aksrt_homepage_has_sidebar(): bool { + $has_widgets = function_exists('aksrt_has_current_sidebar_widgets') + ? aksrt_has_current_sidebar_widgets() + : aksrt_homepage_has_active_sidebars([ + 'all_sidebar_top', + 'home_sidebar', + 'all_sidebar_bottom', + ]); + if (!$has_widgets) { + return false; + } + if (function_exists('aksrt_is_show_sidebar')) { return aksrt_is_show_sidebar(); } - return aksrt_homepage_has_active_sidebars([ - 'all_sidebar_top', - 'home_sidebar', - 'all_sidebar_bottom', - ]); + return true; } } diff --git a/index.php b/index.php index 4697c974..0a229aa9 100644 --- a/index.php +++ b/index.php @@ -21,10 +21,13 @@ get_header();