wordpress中get_sidebar是用來調用模板側邊欄文件sidebar.php,如果模板文件中沒有側邊欄文件sidebar.php,標簽會自動調用程序文件路徑wp-includes/theme-compat/sidebar.php中的默認側邊欄文件,我們可以在模板中創建一個sidebar.php文件,然后通過以下代碼進行調用.
<?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;
- ?>
新聞熱點
疑難解答
圖片精選