當前位置:首頁>WordPress建站>WordPress開發(fā)>WordPress函數(shù):get_sidebar(獲取側(cè)邊欄)

WordPress函數(shù):get_sidebar(獲取側(cè)邊欄)

描述

引入當前主題的模板文件 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)系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學(xué)公眾號 WPDAXUE
WordPress開發(fā)

WordPress函數(shù):get_header(獲取頭部)

2013-4-4 12:03:02

WordPress開發(fā)

WordPress函數(shù):get_footer(獲取頁腳)

2013-4-4 12:29:14

5 條回復(fù) 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 );怎么理解,有沒有在回調(diào)啊?

  2. 為何我用get_sidebar(menu); 怎么不能調(diào)用sidebar-menu.php

    但是get_sidebar(1); 可以調(diào)用sidebar-1.php

    • 試試 get_sidebar(‘menu’);

  3. 你這個函數(shù) 在那里能找到呀

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

?
個人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

金湖县| 济南市| 长乐市| 合肥市| 安仁县| 盐池县| 西林县| 赣州市| 育儿| 荃湾区| 昔阳县| 五大连池市| 油尖旺区| 武乡县| 古田县| 五华县| 新宁县| 龙州县| 扶绥县| 炉霍县| 堆龙德庆县| 新兴县| 湘乡市| 瑞安市| 凌源市| 长宁区| 桃园县| 乌兰县| 犍为县| 恩平市| 玉环县| 临江市| 交口县| 满城县| 中西区| 通海县| 浦县| 长兴县| 房产| 神农架林区| 开阳县|