WordPress中用於建立以及擷取側邊欄的PHP函數講解_php技巧

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.