WordPress函数get_sidebar()用法详解(加载sidebar.php侧边栏模板)

文 / @WordPress主题

WordPress函数get_sidebar()用法详解(加载sidebar.php侧边栏模板)

WordPress的函数get_sidebar用于在WordPress主题中显示边栏(边栏是一个区域,通常位于网站的侧边,用于显示额外的信息,如小工具、搜索框和最新文章)。

要在WordPress主题中使用边栏,首先要在主题文件夹中创建一个名为“sidebar.php”的文件,并在该文件中编写边栏的代码。然后,可以在需要显示边栏的模板文件(如首页模板文件index.php)中调用get_sidebar函数来显示边栏。例如:

<?php get_sidebar(string $name = null); ?>

调用该函数后,WordPress会自动加载主题中的sidebar.php文件,并显示边栏内容。

get_sidebar标签默认加载sidebar.php,但可以通过传递一个参数来实现加载类似sidebar-name.php的模板:

<?php get_sidebar('name'); ?>

以上示例加载sidebar-name.php,如果sidebar-name.php文件不存在,则加载sidebar.php

对于三栏布局,我们需要两个侧边栏,分别为sidebar-left.php和sidebar-right.php,相应的代码如下:

<?php get_header(); ?>
<?php get_sidebar( 'left' ); ?>
<?php get_sidebar( 'right' ); ?>
<?php get_footer(); ?>

注:get_sidebar()函数位于:wp-includes/general-template.php

 

添加UTHEME为好友
扫码添加UTHEME微信为好友
· 分享WordPress相关技术文章,主题上新与优惠动态早知道。
· 微信端最大WordPress社群,限时免费入群。