WordPress主题制作

  1. 主页
  2. 文档
  3. WordPress主题制作
  4. 侧边栏(sidebar)
  5. 显示小工具区域 display new widget areas

显示小工具区域 display new widget areas

范例:

<?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 -->

这篇文章对您有用吗?

我们要如何帮助您?