導讀:WordPress 中 get_sidebar 是用來呼叫模板側邊欄檔案 sidebar.php, 如果模板檔案中沒有側邊欄檔案 sidebar.php, 標籤會自動呼叫程式檔案路徑 wp-inc...
<?php get_sidebar( $name ); ?> 其中的 $name 可以支援設定不同的側邊欄檔名稱, 然後在不同的頁面分別呼叫, 例如:
1 、在模板中建立 sidebar-left.php 檔案, 然後可以透過以下程式碼進行呼叫:
<?php get_sidebar( 'left' ); ?>
2 、在模板檔案中建立 sidebar-right.php 檔案, 可以透過以下程式碼進行呼叫:
<?php get_sidebar( 'right' ); ?>
3 、常用判斷語句混合用法:
<?php
if ( is_home() ) ://如果是首頁
get_sidebar( 'left' );//呼叫 sidebar-left.php 檔案
elseif ( is_404() ) ://如果是 404 頁面
get_sidebar( 'right' );//呼叫 sidebar-right.php 檔案
else ://其他頁面
get_sidebar();//呼叫 sidebar.php 檔案
endif;
?>