Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

zh-cn:get sidebar 获得边栏

描述 Description

引入 你主题文件夹中当前的 sidebar.php 模板文件。

若参数 $name 被指定,则有特定的 sidebar sidebar-{name}.php 被引入。 若 sidebar-{name}.php 并不存在,则会寻找 sidebar.php

如果主题中并不包含 sidebar.php 文件,则会使用默认主题的 sidebar 文件:wp-includes/theme-compat/sidebar.php

使用 Usage

<?php get_sidebar$name ); ?>

参数 Parameters

$name
(string) (optional) Calls for sidebar-name.php. 调用 sidebar-name.php
Default: None

示例 Examples

简单调用 Simple call

假定你的有 wp-content/yourTheme/sidebar-nice-bar.php 文件。那你能在某个页面引入该边栏(sidebar)的方式是

<?php get_header('nice-bar'); ?>

简单的404页面 Simple 404 page

以下是一个应对 "HTTP 404: Not Found" 错误的模板示例,你可以在主题404.php) 中引入。

<?php get_header(); ?>
<h2>Error 404 - Not Found</h2>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

左右边栏 Left and Right Sidebars

在一个主题中使用两个边栏(sidebar)。

<?php get_header(); ?>
<?php get_sidebar( 'left' ); ?>
<?php get_sidebar( 'right' ); ?>
<?php get_footer(); ?>

左右边栏响应的文件名分别是 sidebar-left.phpsidebar-right.php


多边栏 Multi sidebars

对不同页面引用不同的边栏。

<?php
if ( is_home() ) :
  get_sidebar( 'home' );
elseif ( is_404() ) :
  get_sidebar( '404' );
else :
  get_sidebar();
endif;
?>

如上,对应于首页(home)和404的不同边栏分别为 sidebar-home.phpsidebar-404.php

说明 Notes

更改记录 Change Log

  • 引入自WordPress版本: 1.5.0
  • 2.5.0 : 加入了 'name' 参数。

源文件 Source File

get_sidebar()wp-includes/general-template.php 中。

相关阅读 Related

Include Tags

Widgets: is_active_widget(), the_widget(), register_widget(), unregister_widget(), wp_register_widget_control(), wp_unregister_widget_control(), wp_convert_widget_settings(), wp_get_widget_defaults(), wp_widget_description()

See also index of Function Reference and index of Template Tags.