前面给大家介绍了《wordpress主题制作全过程(六):制作footer.php》,本文继续给大家介绍如何制作sidebar.php,下面一起来看一下吧~
制作好了header.php 和 footer.php ,今天我们来制作侧边栏sidebar.php。由于侧边栏的可定制性实在是太强了,所以本节内容比较难,我讲解起来也比较困难,有些内容会被略掉!
作为各个页面公用的侧边栏,我们还是像制作header.php 和 footer.php那样,从index.php中提取侧边栏,放到sidebar.php。好,现在在你的主题目录aurelius下新建文件sidebar.php,从index.php中提取一下代码,放到sidebar.php中:
<!-- column 2 / sidebar --><div class="grid_4"><h3>catagories</h3><ul class="sidebar"><li><a href="">so who are we?</a></li><li><a href="">philosophy</a></li><li><a href="">history</a></li><li><a href="">jobs</a></li><li><a href="">staff</a></li><li><a href="">clients</a></li></ul><h3>archives</h3><ul class="sidebar"><li><a href="">january 2010</a></li><li><a href="">december 2009</a></li><li><a href="">novemeber 2009</a></li><li><a href="">october 2009</a></li><li><a href="">september 2009</a></li><li><a href="">august 2009</a></li></ul></div><div class="hr grid_12 clearfix"> </div>
再用文本编辑器打开index.php、archive.php、page.php和single.php,删掉以上类似代码,改成:
<?php get_sidebar(); ?>
好,现在打开你的博客主页,看看我们制作的主题是否还可以正常工作。现在我们的侧边栏还都是静态的代码,大家可能都知道在wordpress后台 - 外观 - 小工具,那里可以拖动你想要的栏目到侧边栏,但是我们的主题目前还不支持这个功能。现在就让我一起来制作完整的sidebar。
为了适应wordpress程序,我们还要对sidebar.php做一些微调,下载新的样式表style.css,替换aurelius目录下的style.css
开始sidebar.php的制作,我们将在侧边栏放置4个栏目。在初始状态下,也就是你没有在侧边栏放置任何小工具的情况下,这4个栏目自上而下为分类目录、最新文章、标签云和文章月存档。现在将sidebar.php中所有代码删除,改成:
<!-- column 2 / sidebar --><div class="grid_4"><?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('first_sidebar') ) : ?><h3>分类目录</h3><ul><?php wp_list_categories('depth=1&title_li=&orderby=id&show_count=0&hide_empty=1&child_of=0'); ?></ul><?php endif; ?><?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('second_sidebar') ) : ?><h3>最新文章</h3><ul><?php$posts = get_posts('numberposts=6&orderby=post_date');foreach($posts as $post) {setup_postdata($post);echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';}$post = $posts[0];?></ul><?php endif; ?><?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('third_sidebar') ) : ?><h3>标签云</h3><p><?php wp_tag_cloud('smallest=8&largest=22'); ?></p><?php endif; ?><?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('fourth_sidebar') ) : ?><h3>文章存档</h3><ul><?php wp_get_archives('limit=10'); ?></ul><?php endif; ?></div><div class="hr grid_12 clearfix"> </div>
然后点此处下载functions.php放到主题目录aurelius下,这时候你的侧边栏就可以正常工作了,在wordpress后台 - 外观 - 小工具,可以正常地拖动小工具到侧边栏了。
好了,sidebar.php到此就制作成功了。这节内容理论的东西讲得不多,关键是我不知道怎么去表述这些东西,就算我说出来,可能你也不会太明白了。
推荐学习:《wordpress教程》
以上就是wordpress主题制作全过程(七):制作sidebar.php的详细内容。