这个是虚坏叔叔的主站。牢记域名: [xuhss.com](拼音首字母)。首页可以搜索或: 虚幻账号

WordPress模板层次09:sidebar.php

模板层次 虚幻 887℃ 0评论

get_sidebar()函数

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

调用方式一

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

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

它将加载sidebar.php:

<div class="col-md-4">
	<?php if ( ! dynamic_sidebar( 'primary' ) ): ?>/***加载primary小工具作为侧边栏***/
	<h3>Widget Setup</h3>
	<p>Please add widgets to the page widget to have them display here</p>
	<?php endif; ?>
</div>

调用方式二

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

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

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

<div class="col-md-4">
	<?php if ( ! dynamic_sidebar( 'blog' ) ): ?>/***加载blog小工具作为侧边栏***/
	<h3>Widget Setup</h3>
	<p>Please add widgets to the page widget to have them display here</p>
	<?php endif; ?>
</div>

这说明了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

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

转载请注明:虚坏叔叔 » WordPress模板层次09:sidebar.php

喜欢 (4)

您必须 登录 才能发表评论!