wordpress get_sidebar()函数


【函数介绍】

get_sidebar()函数是用于包含我们主题中的sidebar.php模板文件。你也可以指定自己的边栏文件名,命名格式为:sidebar-{$name}.php。
说明:如果主题中没有sidebar.php文件,那么函数会包含来自默认主题wp-content/themes/default/sidebar.php的侧边栏文件。

【函数使用】

  1. <?php get_sidebar( $name ); ?>

【参数说明】

$name
(string) (可选) 边栏php文件名sidebar-name.php.
Default: None

【函数实例】

404页面(你主题下的404.php)模板使用get_sidebar()获取边栏文件:

  1. <?php get_header(); ?>
  2. <h2>Error 404 - Not Found</h2>
  3. <?php get_sidebar(); ?>
  4. <?php get_footer(); ?>

调用左边栏与右边栏:

  1. <?php get_header(); ?>
  2. <?php get_sidebar('left'); ?>
  3. <?php get_sidebar('right'); ?>
  4. <?php get_footer(); ?>

不同页面上的不同侧边栏

  1. <?php
  2. if ( is_home() ) :
  3. get_sidebar('home');
  4. elseif ( is_404() ) :
  5. get_sidebar('404');
  6. else :
  7. get_sidebar();
  8. endif;
  9. ?>

【源文件】

get_sidebar()位于wp-includes/general-template.php中。