正在檢視 1 個帖子:1-1 (共 1 個帖子)
-
作者帖子
-
如果你想在你的主題中新增/註冊小工具區域,可以按照如下步驟進行。
在 function.php 檔案中註冊小工具區域
你可以註冊多個小工具區域,複製如下程式碼到主題的 function.php 中即可:/** * 給 WordPress 主題新增全新的自定義小工具 * https://bbs.weixiaoduo.com/topic/23944 */ function weixiaoduo_widgets_init() { register_sidebar( array( 'name' => __( ' 全站頂部廣告', 'weixiaoduo' ), 'id' => 'sidebar-1', 'description' => __( ' 這裡可以讓全站顯示公告', 'weixiaoduo' ), 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', ) ); } add_action( 'widgets_init', 'weixiaoduo_widgets_init' );
儲存之後,前往後臺的小工具頁面,就能看到對應的小工具區域啦:
注意修改程式碼中小工具標題 (name) 、描述 (description) 及 (id) 。
模版中呼叫小工具區域
到目前為止,小工具區域已經在後臺能夠看到,但是並不會在主題中顯示出來,如果你想在主題中呼叫對應的小工具區域,將如下程式碼貼上到主題檔案中對應的位置即可:
/** *模版中呼叫 WordPress 小工具區域 * https://bbs.weixiaoduo.com/topic/23944 */ <?php if ( is_active_sidebar( 'sidebar-id' ) ) : ?> <div id="div-id" class="div-widget-area"> <?php dynamic_sidebar( 'sidebar-id' ); ?> </div> <?php endif; ?>
注意 sidebar-id 與 function.php 中註冊的 id 對應。
-
作者帖子
正在檢視 1 個帖子:1-1 (共 1 個帖子)
- 哎呀,回覆話題必需登入。