WordPress模板层次09:sidebar.php

get_sidebar()函数

关于侧边栏,很重要的一点是,它会通过函数get_sidebar调用出来的。get_sidebar可以接受不同的参数,来实现对侧边栏的灵活控制。

调用方式一

查看front-page.php文件,可以看到调用getsidebar():

WordPress 侧边栏 xuhss.com01 - WordPress模板层次09:sidebar.php

它将加载sidebar.php:

/***加载primary小工具作为侧边栏***/

Widget Setup

Please add widgets to the page widget to have them display here

调用方式二

但是,如果打开home.php文件,并且向下滚动到 get_sidebar(‘blog’) 。

WordPress 侧边栏 xuhss.com02 - WordPress模板层次09:sidebar.php

那么,它将会加载一个名为sidebar-blog.php的文件:

/***加载blog小工具作为侧边栏***/

Widget Setup

Please add widgets to the page widget to have them display here

这说明了getsidebar函数能够根据不同的参数加载不同的文件,来调用不同的内容来生成侧边栏。

实际上,get_header和get_footer也是如此,只不过它们不那么常用而已。

primary小工具和blog小工具的定义

细心的你可能会注意到:

  • sidebar.php中调用 primary小工具 作为侧边栏
  • sidebar-blog.php中调用 blog小工具 作为侧边栏

实际上,这两个小工具需要事先被创建,它们在functions.php中创建:

WordPress 小工具创建 xuhss.com01 - WordPress模板层次09:sidebar.php

有了这段代码,网站的后台就会显示不同的侧边栏。

WordPress 小工具创建 xuhss.com02 - WordPress模板层次09:sidebar.php

所以,可以来到这里编辑小工具,并通过代码控制侧边栏显示的内容。

  • 本文固定链接: http://www.xuhss.com/theme-dev/template-hierarchy/sidebar-php
  • 转载请注明: MrBang 于 虚幻私塾 发表

你可能感兴趣的:(WordPress模板层次09:sidebar.php)