php - Wordpress 动态页脚小部件 - 仅当一个小部件存在时才显示第二个小部件区域,否则显示 1
问题描述
我正在我的 WordPress 主题的页脚中创建一个动态小部件区域。
我想要做的是,创建 4 个小部件,但仅在存在一个时才显示下一个。
所以默认情况下,我会看到一个名为 footer one 的小部件
如果我们添加页脚,则会显示一个页脚 2 选项,但是在页脚中,我们将看到一个页脚小部件如果我们在页脚 2 中添加第二个小部件,我们将看到小部件 3。如果我们在页脚 3 中添加一个小部件,我们将看到页脚4.
如果只有页脚 1 存在宽度 100% 如果 2 存在页脚 1 50%,我需要设置样式。页脚 2 50%
如果存在 3 页脚 1 33% 页脚 2 33% 。页脚 3 33%
如果存在 4 页脚 1 25% 页脚 2 25% 页脚 3 25% 页脚 4 25%
重要的是,我设法完成了这项工作。但如果我仍然无法让其他小部件仅在父小部件被填充时显示。
这是我的代码:
<div class="footer-widgets">
<?php
/* The footer widget area is triggered if any of the areas
* have widgets. So let's check that first.
*
* If none of the sidebars have widgets, then let's bail early.
*/
if ( ! is_active_sidebar( 'first-footer-widget-area' )
&& ! is_active_sidebar( 'second-footer-widget-area' )
&& ! is_active_sidebar( 'third-footer-widget-area' )
&& ! is_active_sidebar( 'fourth-footer-widget-area' )
)
return;
if ( is_active_sidebar( 'first-footer-widget-area' )
&& is_active_sidebar( 'second-footer-widget-area' )
&& is_active_sidebar( 'third-footer-widget-area' )
&& is_active_sidebar( 'fourth-footer-widget-area' )
) : ?>
<aside class="fatfooter" role="complementary">
<div class="first quarter left widget-area">
<?php dynamic_sidebar( 'first-footer-widget-area' ); ?>
</div><!-- .first .widget-area -->
<div class="second quarter widget-area">
<?php dynamic_sidebar( 'second-footer-widget-area' ); ?>
</div><!-- .second .widget-area -->
<div class="third quarter widget-area">
<?php dynamic_sidebar( 'third-footer-widget-area' ); ?>
</div><!-- .third .widget-area -->
<div class="fourth quarter right widget-area">
<?php dynamic_sidebar( 'fourth-footer-widget-area' ); ?>
</div><!-- .fourth .widget-area -->
</aside><!-- #fatfooter -->
<?php
elseif ( is_active_sidebar( 'first-footer-widget-area' )
&& is_active_sidebar( 'second-footer-widget-area' )
&& is_active_sidebar( 'third-footer-widget-area' )
&& ! is_active_sidebar( 'fourth-footer-widget-area' )
) : ?>
<aside class="fatfooter" role="complementary">
<div class="first one-third left widget-area">
<?php dynamic_sidebar( 'first-footer-widget-area' ); ?>
</div><!-- .first .widget-area -->
<div class="second one-third widget-area">
<?php dynamic_sidebar( 'second-footer-widget-area' ); ?>
</div><!-- .second .widget-area -->
<div class="third one-third right widget-area">
<?php dynamic_sidebar( 'third-footer-widget-area' ); ?>
</div><!-- .third .widget-area -->
</aside><!-- #fatfooter -->
<?php
elseif ( is_active_sidebar( 'first-footer-widget-area' )
&& is_active_sidebar( 'second-footer-widget-area' )
&& ! is_active_sidebar( 'third-footer-widget-area' )
&& ! is_active_sidebar( 'fourth-footer-widget-area' )
) : ?>
<aside class="fatfooter" role="complementary">
<div class="first half left widget-area">
<?php dynamic_sidebar( 'first-footer-widget-area' ); ?>
</div><!-- .first .widget-area -->
<div class="second half right widget-area">
<?php dynamic_sidebar( 'second-footer-widget-area' ); ?>
</div><!-- .second .widget-area -->
</aside><!-- #fatfooter -->
<?php
elseif ( is_active_sidebar( 'first-footer-widget-area' )
&& ! is_active_sidebar( 'second-footer-widget-area' )
&& ! is_active_sidebar( 'third-footer-widget-area' )
&& ! is_active_sidebar( 'fourth-footer-widget-area' )
) :
?>
<aside class="fatfooter" role="complementary">
<div class="first full-width widget-area">
<?php dynamic_sidebar( 'first-footer-widget-area' ); ?>
</div><!-- .first .widget-area -->
</aside><!-- #fatfooter -->
<?php //end of all sidebar checks.
endif;?>
</div>
解决方案
您可以在 Wordpress PHP 中使用 If else 并可以在其基础上打印 CSS...只需参考一个返回活动小部件数量的钩子...
这可以帮助您构建一个函数来获取活动小部件:https ://generatewp.com/snippet/2V0V0gy/
谢谢!
推荐阅读
- python - 无法在 azureML 笔记本中获取对计算实例的引用
- javascript - 在 Node.js 中格式化 nodepad 文件
- react-native - 反应原生 webview 不加载动态内容
- sql - SQL选择具有相同列值的所有项目
- python - 从 pandas 数据框中删除大量行的算法效率
- javascript - 如何覆盖左侧边栏并将其他组件向右推?
- python - 魔术方法可以返回不同的结果类型吗?
- sql - 在 SQL Server 中执行查询时出错
- c - realloc 因指针数组而失败
- node.js - 尝试返回异步'express-asyncify'的http状态代码时,“无法读取未定义的属性'发送'”