描述
引入當前主題的模板文件 sidebar.php 。如果使用特定的名字 ($name) ,那么就會引用包含這個特定名字的模板文件 sidebar-{name}.php 。
如果主題沒有 sidebar.php 文件,就會引入默認的 wp-includes/theme-compat/sidebar.php 。
用法
<?php get_sidebar( $name ); ?>
參數(shù)
- $name
- (string) (可選) 調(diào)用 sidebar-name.php.
- 默認: None
例子
404頁面
下面的代碼是一個簡單模板文件,專門用來顯示 “HTTP 404: Not Found” 錯誤的 (這個文件應(yīng)該包含在你的主題中,名為 404.php)
<?php get_header(); ?>
<h2>Error 404 - Not Found</h2>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
左邊欄和右邊欄
一個主題包含2個邊欄。
<?php get_header(); ?>
<?php get_sidebar('left'); ?>
<?php get_sidebar('right'); ?>
<?php get_footer(); ?>
右邊欄和左邊欄的名字應(yīng)該分別命名為 sidebar-right.php 和 sidebar-left.php。
多個邊欄
不同頁面使用不同邊欄
<?php
if ( is_home() ) :
get_sidebar( 'home' );
elseif ( is_404() ) :
get_sidebar( '404' );
else :
get_sidebar();
endif;
?>
首頁和404頁面專用的邊欄的名字應(yīng)該分別為 sidebar-home.php 和 sidebar-404.php。
資源文件
get_sidebar() 包含在 wp-includes/general-template.php.
相關(guān)函數(shù)
get_header(), get_footer(), get_template_part(), get_search_form(),comments_template()
聲明:本站所有文章,如無特殊說明或標注,均為本站原創(chuàng)發(fā)布。任何個人或組織,在未征得本站同意時,禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進行處理。





function get_sidebar( $name = null ) {
/**
* Fires before the sidebar template file is loaded.
*
* The hook allows a specific sidebar template file to be used in place of the
* default sidebar template file. If your file is called sidebar-new.php,
* you would specify the filename in the hook as get_sidebar( ‘new’ ).
*
* @since 2.2.0
* @since 2.8.0 $name parameter added.
*
* @param string $name Name of the specific sidebar file to use.
*/
do_action( ‘get_sidebar’, $name );
$templates = array();
$name = (string) $name;
if ( ” !== $name )
$templates[] = “sidebar-{$name}.php”;
$templates[] = ‘sidebar.php’;
// Backward compat code will be removed in a future release
if (” == locate_template($templates, true))
load_template( ABSPATH . WPINC . ‘/theme-compat/sidebar.php’);
}
中do_action( ‘get_sidebar’, $name );怎么理解,有沒有在回調(diào)啊?
為何我用get_sidebar(menu); 怎么不能調(diào)用sidebar-menu.php
但是get_sidebar(1); 可以調(diào)用sidebar-1.php
試試 get_sidebar(‘menu’);
你這個函數(shù) 在那里能找到呀
get_sidebar() 包含在 wp-includes/general-template.php.