fix: collapse empty homepage sidebar

This commit is contained in:
kate522 committed 2026-08-14 14:12:17 +08:00
1 parent f6134f4458
commit 15b4c818c5
3 files changed
+46 -11

No files matched your search

+28 -3
View File
@@ -54,6 +54,29 @@ function aksrt_resolve_sidebar_id(): string {
} }
endif; 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')): if (!function_exists('aksrt_get_post_show_layout')):
/** /**
* Zibll-compatible post/page layout override. * Zibll-compatible post/page layout override.
@@ -218,9 +241,7 @@ function aksrt_get_site_layout(): int {
if (is_single()) { if (is_single()) {
$post_id = (int) get_queried_object_id(); $post_id = (int) get_queried_object_id();
$layout = aksrt_get_post_show_layout($post_id); $layout = aksrt_get_post_show_layout($post_id);
$has_article_widgets = is_active_sidebar('all_sidebar_top') $has_article_widgets = aksrt_has_current_sidebar_widgets();
|| is_active_sidebar('sidebar-article')
|| is_active_sidebar('all_sidebar_bottom');
// Keep the sidebar in the document whenever it is configured. CSS handles // Keep the sidebar in the document whenever it is configured. CSS handles
// narrow viewports; wp_is_mobile() is User-Agent based and can otherwise // narrow viewports; wp_is_mobile() is User-Agent based and can otherwise
// suppress widgets on a desktop-sized page. // suppress widgets on a desktop-sized page.
@@ -243,6 +264,10 @@ function aksrt_get_site_layout(): int {
return 1; return 1;
} }
if (!aksrt_has_current_sidebar_widgets()) {
return 1;
}
$position = 'right'; $position = 'right';
if (is_singular()) { if (is_singular()) {
$meta = aksrt_get_post_show_layout(get_queried_object_id()); $meta = aksrt_get_post_show_layout(get_queried_object_id());
+11 -4
View File
@@ -146,14 +146,21 @@ if (!function_exists('aksrt_homepage_has_sidebar')) {
* Whether the fixed homepage should render an aside column. * Whether the fixed homepage should render an aside column.
*/ */
function aksrt_homepage_has_sidebar(): bool { function aksrt_homepage_has_sidebar(): bool {
if (function_exists('aksrt_is_show_sidebar')) { $has_widgets = function_exists('aksrt_has_current_sidebar_widgets')
return aksrt_is_show_sidebar(); ? aksrt_has_current_sidebar_widgets()
} : aksrt_homepage_has_active_sidebars([
return aksrt_homepage_has_active_sidebars([
'all_sidebar_top', 'all_sidebar_top',
'home_sidebar', 'home_sidebar',
'all_sidebar_bottom', 'all_sidebar_bottom',
]); ]);
if (!$has_widgets) {
return false;
}
if (function_exists('aksrt_is_show_sidebar')) {
return aksrt_is_show_sidebar();
}
return true;
} }
} }
+7 -4
View File
@@ -21,10 +21,13 @@ get_header();
<?php <?php
// Zibll dual-column: sidebar is a sibling of .content-wrap under main.container. // Zibll dual-column: sidebar is a sibling of .content-wrap under main.container.
// Visibility is driven by body.site-layout-* (aksrt_is_show_sidebar), not widget presence. // A sidebar column is useful only when visibility is enabled and at least one
$has_sidebar = function_exists('aksrt_is_show_sidebar') // of its global/page-specific widget zones has content.
? aksrt_is_show_sidebar() $has_sidebar_widgets = function_exists('aksrt_has_current_sidebar_widgets')
: is_active_sidebar('home_sidebar'); ? aksrt_has_current_sidebar_widgets()
: (is_active_sidebar('all_sidebar_top') || is_active_sidebar('home_sidebar') || is_active_sidebar('all_sidebar_bottom'));
$has_sidebar = (function_exists('aksrt_is_show_sidebar') ? aksrt_is_show_sidebar() : true)
&& $has_sidebar_widgets;
?> ?>
<main id="main-content" class="site-main container" tabindex="-1"> <main id="main-content" class="site-main container" tabindex="-1">
<div class="content-wrap"> <div class="content-wrap">