企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
【5.小工具】 对于共同使用的部分,如header.php,sidebar.php,footer.php,这时候不适合使用shortcode来解决在后台更新,而使用小工具就是不错的方案。 functions.php ~~~ //add our widget locations function ourWidgetsInit(){ register_sidebar( array( 'name' => 'Sidebar', 'id' => 'sidebar1', 'before_widget' => '<div class="widget-item">', 'after_widget' => '</div>', 'before_title' => '<h4 class="my-special-class">', 'after_title' =>'</h4>' )); register_sidebar( array( 'name' => 'Footer Area 1', 'id' => 'footer1' )); register_sidebar( array( 'name' => 'Footer Area 2', 'id' => 'footer2' )); register_sidebar( array( 'name' => 'Footer Area 3', 'id' => 'footer3' )); register_sidebar( array( 'name' => 'Footer Area 4', 'id' => 'footer4' )); } add_action('widgets_init', 'ourWidgetsInit'); ~~~ 页面显示小工具: footer.php ~~~ <!-- footer-widgets --> <div class="footer-widgets"> <?php if (is_active_sidebar('footer1')) : ?> <?php dynamic_sidebar('footer1'); ?> <?php endif; ?> <?php if (is_active_sidebar('footer2')) : ?> <?php dynamic_sidebar('footer2'); ?> <?php endif; ?> <?php if (is_active_sidebar('footer3')) : ?> <?php dynamic_sidebar('footer3'); ?> <?php endif; ?> <?php if (is_active_sidebar('footer4')) : ?> <?php dynamic_sidebar('footer4'); ?> <?php endif; ?> </div><!-- /footer-widgets --> ~~~ `is_active_sidebar()`函数用来检查侧边栏里是否有小工具,如果有则返回 True,没有则返回 False。