站长资源
中国站长网站

WordPress模板包含标签:get_sidebar()详解

描述

引入当前主题的模板文件 sidebar.php 。如果使用特定的名字 ($name) ,那么就会引用包含这个特定名字的模板文件 sidebar-{name}.php 。

如果主题没有 sidebar.php 文件,就会引入默认的 wp-includes/theme-compat/sidebar.php 。

用法

<?php get_sidebar( $name ); ?>

参数

$name
(string) (可选) 调用 sidebar-name.php.
默认: None

例子

404页面

下面的代码是一个简单模板文件,专门用来显示 “HTTP 404: Not Found” 错误的 (这个文件应该包含在你的主题中,名为 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(); ?>

右边栏和左边栏的名字应该分别命名为 sidebar-right.php 和 sidebar-left.php。

多个边栏

不同页面使用不同边栏

<?php
if ( is_home() ) :
	get_sidebar( 'home' );
elseif ( is_404() ) :
	get_sidebar( '404' );
else :
	get_sidebar();
endif;
?>

首页和404页面专用的边栏的名字应该分别为 sidebar-home.php 和 sidebar-404.php。

资源文件

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

相关函数

get_header(), get_footer(), get_template_part(), get_search_form(),comments_template()

本文出处:来自互联网信息共享,请勿相信收费信息站长资源 » WordPress模板包含标签:get_sidebar()详解

评论 抢沙发

评论前必须登录!