外贸独立站的底层设计密码WordPress 成品站模板
当前位置:首页>WordPress建站>WordPress开发>WordPress函数:get_sidebar(获取侧边栏)

WordPress函数:get_sidebar(获取侧边栏)

描述

引入当前主题的模板文件 sidebar.php 。如果使用特定的名字 ($name) ,那么就会引用包含这个特定名字的模板文件 sidebar-{name}.php 。

如果主题没有 sidebar.php 文件,就会引入默认的 wp-includes/theme-compat/sidebar.php 。

用法

<?php get_sidebar( $name ); ?> 

参数

$name

(string) (可选) 调用 sidebar-name.php.

默认: None

例子

404页面

下面的代码是一个简单模板文件,专门用来显示 “HTTP 404: Not Found” 错误的 (这个文件应该包含在你的主题中,名为 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(); ?>

右边栏和左边栏的名字应该分别命名为 sidebar-right.php 和 sidebar-left.php。

多个边栏

不同页面使用不同边栏

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

首页和404页面专用的边栏的名字应该分别为 sidebar-home.php 和 sidebar-404.php。

资源文件

get_sidebar() 包含在 wp-includes/general-template.php.

相关函数

get_header(), get_footer(), get_template_part(), get_search_form(),comments_template()

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
WordPress开发

WordPress函数:get_header(获取头部)

2013-4-4 12:03:02

WordPress开发

WordPress函数:get_footer(获取页脚)

2013-4-4 12:29:14

5 条回复 A文章作者 M管理员
  1. 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 );怎么理解,有没有在回调啊?

  2. 为何我用get_sidebar(menu); 怎么不能调用sidebar-menu.php

    但是get_sidebar(1); 可以调用sidebar-1.php

    • 试试 get_sidebar(‘menu’);

  3. 你这个函数 在那里能找到呀

    • get_sidebar() 包含在 wp-includes/general-template.php.

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索