给WordPress模板制作者的7个建议

本文转载自WordPress啦!

1. 了解所有的模板标签

WordPress编写代码时一定要注意模板标签。当然你也可以通过简单的PHP代码实现同样的效果,不过,还是尽可能使用WordPress的模板标签。

模板标签参考指南: http://codex.wordpress.org/Template_Tags

2.了解所有的WordPress 选项

主题制作者经常犯的一个错误就是忽略了WordPress选项。 你可能会问,那是啥?呵呵,我说的就是WordPress后台设置里可以设置的默认选项。 举一个简单的例子:

很多主题都有固定的发布日期和时间格式:

<span><?php the_time(‘F j, Y \a\t G:i’); ?></span>

访客看到的效果是这样的: June 10, 2009 at 10:53

但是,要是博客管理员想要另外的时间显示格式呢?他会找到设置下的一般选项然后修改格式,但是最终显示的却还是旧的格式。

正确的做法是运用下面的代码:

<?php
$dateformat = get_option(‘date_format’);
$timeformat = get_option(‘time_format’);
the_time(“$dateformat \a\\t $timeformat”); ?>

这样管理员才能真正控制要显示的时间格式,为你的客户减少不少烦恼,帮助论坛中的客户提交Ticket也就随之减少。

选项参考指南: http://codex.wordpress.org/Option_Reference

3. 了解所有的WordPress函数

如果你想对博客布局做大的修改,你得对WordPress的很多函数和参数都要很了解。

有时候,因为你有几年的PHP经验,就会决定用自己的方法去写函数,以为可以取得更好的效果,殊不知这是在浪费时间。始终使用WordPress的功能,对你的用户来说帮助更大。

参考: http://codex.wordpress.org/Function_Reference

4. 给核心的WordPress边栏附件添加基本的样式

如果你计划开发多款主题,我建议你给一些基本(但流行)的WordPress边栏附件创建共同的样式模板,如:文档,日历,搜索,标签云等。

WordPress这些附件一般都使用相同的ID 和类别,因此在你的样式表中不难找到它们。就算它们与你的主题设计不是很搭配,还是尽量这么做。你会发现它会为你省力不少。

5. 不要依靠第三方的插件和附件

这也是另一个常见的错误:主题制作者强迫用户安装一些插件。更糟糕的是,都不检查内置的功能。 例如,使用WP-Pagenavi翻页功能来取代WordPress默认的翻页。

错误做法:

<?php SEO_pager(); ?>

在这个例子中,如果没有安装该插件,此行结束后WordPress 将停止执行任何东西,这就是为什么一些主题会莫名其妙出错的原因。

正确做法:

<?php if (function_exists(‘SEO_pager’)) { SEO_pager(); } ?>

6. 给所有元素创建模板文件

很多主题仅使用到一些模板文件:archive.php, index.php, page.php, search.php, single.php.就这么多,完了。

其实,还可以通过创建其它很多模板文件改善主题的整体体验。如样式模板: author.php, attachment.php, video.php,等。

参考: 模板层次图http://codex.wordpress.org/Template_Hierarchy#Visual_Overview

7.创建一个主题选项(控制面板)页面

如果你还没有使用它,那么现在按我说的做。在给你的主题完成第一个控制面板页面的时候,你就会意识到它还是让你轻松不少。

附原文:

7 Essential Tips for WordPress Theme Developers

When I first started with WordPress (in March 2009) I already had over 3 years of PHP experience. I was used to developing my own CMS engines for my clients, so the first days with WordPress were rather curious: changing core WordPress functions, templates, etc.

After that, with the help of Pavel (@ciorici), I decided to play by the rules: learn The WordPress Way.
Think the way WordPress thinks, instead of working around it.

I hope this list of advices will help starting WordPress theme developers as well as some more experienced ones.

1. Learn all Template tags

When coding a design into WordPress, you really should pay attention to Template Tags. Of course you can achieve the same thing by simple php coding, however, it is better to stick to WordPress Template Tags as much as you can.

For reference: http://codex.wordpress.org/Template_Tags

2. Learn all WordPress options

A common “mistake” of theme developers(coders) is that they forget about WordPress Options.
What is that you may ask? Well, remember all those default options that can be set in Dashboard > Settings? That’s what I’m talking about.

Let me give you a simple example:
Many themes have hardcoded the format of post date and time:

1.<span><?php the_time('F j, Y \a\t G:i'); ?></span>

The visitor in turn would see something like this: June 10, 2009 at 10:53

However, what if the blog’s administrator would like to have a different time format? He goes to Settings > General, changes the format and… nothing. The theme still shows the old format.

The correct way to do it would be similar to this:

1.<?php
2.$dateformat = get_option('date_format');
3.$timeformat = get_option('time_format');
4.the_time("$dateformat \a\\t $timeformat"); ?>

Now the administrator is in full control over how the date and time is shown. Less frustration for your clients, less support tickets in the forums :)

For reference: http://codex.wordpress.org/Option_Reference

3. Learn all WordPress functions

If you would like to make major changes in the basic layout of a blog, you will probably have to rely on many functions and conditionals.
Sometimes, if you have a couple of years of PHP experience (guilty), you would decide to “make things better” and end up writing many functions of your own.
That will steal time [and flexibility].
Always try to rely on core WordPress functions. Your customers will thank you later.

For reference: http://codex.wordpress.org/Function_Reference

4. Add basic styling to core WordPress widgets

If you plan on developing more than 1 theme, I recommend creating common style templates for a couple of basic (but popular) WordPress widgets, such as: Archives, Calendar, Search, Tag Cloud, etc.
WordPress always uses same ID and Class for these widgets, so it is not hard to find them and include in your stylesheets.

Even if they don’t quite fit into your theme design, try to do that. That will earn you some karma points.

5. Don’t rely on third-party plug-ins and widgets

This is another common mistake: theme developers “assume” that users will have certain plug-ins installed. To make matters worse, they include functions from these plug-ins without checking for their presence.

For example if you want to use the WP-Pagenavi navigation instead of the default WordPress navigation.

Doing it wrong:

1.<?php SEO_pager(); ?>

In this case, if the plug-in is not installed, WordPress will stop executing anything after this line. This is the reason why some themes “break” unexpectedly.

Doing it right:

1.<?php if (function_exists('SEO_pager'))  {
2. SEO_pager(); } ?>
6. Create template files for all elements

Most themes use just a couple of templates file: archive.php, index.php, page.php, search.php, single.php. That’s it.
Well, there are many other templates that could improve the overall experience with your theme. You might want to style templates like: author.php, attachment.php, video.php, etc.

Reference: Template Hierarchy Diagram http://codex.wordpress.org/Template_Hierarchy#Visual_Overview

7. Create a Theme options (control panel) page

If you’re not using them already, let me tell you this: after you complete your first options page for your theme, you will realize how much freedom that gives you.

Some tutorials to get your started:

Leave a Reply