范例:
<?php
if ( is_active_sidebar( 'sidebar-1' ) ) {
?>
<div class="widget-column footer-widget-1">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</div>
<?php
}
?>
说明:
1、 判断小工具区域是否激活:
“sidebar-1”是在主题文件中通过注册侧边栏:register_sidebar()定义的小工具区域(widgets area)
通过判断函数 is_active_sidebar(‘sidebar-1’ ) 判断是否激活(后台激活)。
2、已注册:使用dynamic_sidebar
显示
3、 可以直接把相关代码直接放到相关模板文件中,也可以构建模板文件
构建模板文件
一般侧边栏模板文件名为sidebar.php,也可以增加后缀: sidebar-footer.php。
sidebar.php(或sidebar-xxx.php),在需要使用的模板文件中调用sidebar.php。
调用模板文件
<?php get_sidebar(); ?>
或
<?php get_sidebar(‘xxx’); ?>
也可以加后缀:sidebar-xxx.php。
调用模板文件,在模板文件中输入如下命令,可以调用sidebar.php
<?php get_sidebar(); ?>
如果调用 sidebar-xxx.php
<?php get_sidebar('xxx'); ?>
如下是sidebar.php范例
<?php
if ( ! is_active_sidebar( 'sidebar-1' ) ) {
return;
}
?>
<aside id="secondary" class="widget-area sidebar-1-area mt-3r card">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside><!-- #secondary -->