這篇文章主要介紹了WordPress中用于創建以及獲取側邊欄的PHP函數講解,分別為register_sidebar()函數和get_sidebar()的使用,需要的朋友可以參考下
register_sidebar()(創建側邊欄)
建立一個側邊欄,用來放置小工具。這個函數使用的時候請放在一個函數里,掛載到 “widgets_init” 鉤子。
用法
register_sidebar( $args );
參數
$args
(字符串 | 數組)(可選)要創建的側邊欄的參數。
默認值:
- $args = array(
- 'name' => __( 'Sidebar name', 'theme_text_domain' ),
- 'id' => 'unique-sidebar-id',
- 'description' => '',
- 'class' => '',
- 'before_widget' => '<li id="%1" class="widget %2">',
- 'after_widget' => '</li>',
- 'before_title' => '<h2 class="widgettitle">',
- 'after_title' => '</h2>'
- );
數組參數介紹:
name:側邊欄名稱
id:側邊欄 ID,必須為小寫,默認為遞增的數組 ID
description:側邊欄描述
class:給其中的小工具的額外 class
before_widget:里邊的小工具的開頭 Html 代碼
after_widget:里邊的小工具的末尾的 Html 代碼
before_title:里邊的小工具的標題的開頭 Html 代碼
after_title:里邊的小工具的標題的末尾的 Html 代碼
例子
- register_sidebar( array(
- 'name' => __( '右邊的側邊欄' ),
- 'id' => 'sidebar-1',
- 'description' => __( '右側邊欄的小工具。' ),
- 'before_title' => '<h3 class="title">',
- 'after_title' => '</h3 class="title">',
- ));
其它
該函數位于:wp-includes/widgets.php
get_sidebar()(獲取側邊欄)
get_sidebar() 用來引入側邊欄模板。如果指定名稱則引入當前主題根目錄的 sidebar-{name}.php 文件,不指定則引入當前主題根目錄的 sidebar.php 文件,如果文件不存在則引入 wp-includes/theme-compat/sidebar.php 文件。
用法
get_sidebar( $name );
參數
$name
(字符串)(可選)引入模板的名稱,如果指定則引入當前主題根目錄的 sidebar-{$name}.php 文件。
默認值:None
例子
下邊的代碼將引入當前主題根目錄的 sidebar.php 文件:
<?php get_sidebar(); ?>
下邊的代碼將引入當前主題根目錄的 sidebar-left.php 文件:
<?php get_sidebar( 'left' ); ?>
下邊的例子分別引入了左側邊欄(sidebar-left.php)和右側邊欄(sidebar-right.php):
- <?php get_header(); ?>
- <?php get_sidebar( 'left' ); ?>
內容內容
<?php get_sidebar( 'right' ); ?>
<?php get_footer(); ?>
其它
此函數位于:wp-includes/general-template.php
新聞熱點
疑難解答
圖片精選