WordPress:Stepping Into Template Tags

来自站长百科
Fludlen讨论 | 贡献2008年4月7日 (一) 16:48的版本
跳转至: 导航、​ 搜索

If you take a peek into the header.php template file that came with your WordPress Theme, you will notice that where it says "My Blog Name", whatever it is, when you view your WordPress site, it doesn't say "My Blog Name" in the template file. In fact, it has a bunch of strange arrows and parentheses and words that don't make much sense.

你如果看看你的WordPress 主题上的header.php 模板文件,你就会注意到,在显示"我的博客名"的地方,不管它是什么,当你访问你的WordPress站点时,它在模板文件中不再显示"我的博客名"。事实上,它有一大堆的奇怪的箭头和括号和字母,都没有什么意思。

This is an example of a Template Tag. 这是一个模板标签的一个例子。


Let's take a few steps toward learning more about what these are and how they work. 让我们在采取一些措施,更深地学习这些是什么,它们是如果运行的。

What is a Template Tag

一个模板标签是什么

A template tag is code that instructs WordPress to "do" or "get" something. In the case of the header.php template tag for your WordPress site's name, it looks like this:

一个模板标签是一个代码来指示WordPress"去做" 或者 "得到"一些东西。对于你的WordPress站点名的header.php模板标签,它看起来是这样的:

<h1><?php bloginfo('name'); ?></h1>
<h1><?php bloginfo('name'); ?></h1>


The template tag is <?php bloginfo(); ?> wrapped in an H1 heading tag. The bloginfo() tag gets information from your User Profile and Options > General in the Wordpress:Administration Panels. In the example here, the word name inside of the quote marks in the tag instructs the tag to "get the blog's site name". This is called a parameter.

模板标签 <?php bloginfo(); ?> 包在一个 H1 标题标签中, bloginfo()标签从你的管理面板中的

用户个人基本资料选项 > 一般 得到 信息。

Template Tag Parameters

模板标签参数

In addition to the name parameter in the <?php bloginfo(); ?> template tag, there is other information that can be displayed. Let's look at a few of these parameters - and you can find more information and examples on the bloginfo() Codex page.

除了在<?php bloginfo(); ?>模板标签上的名称 参数外,也可以显示其它的信息。然我们来看看这些参数中的其中的几个-在bloginfo() Codex 页面上你可以找到更多的信息和例子。


name <?php bloginfo('name'); ?>
As mentioned, this displays the name of the site and is set by the administrator in the Options > General SubPanel by default.
名称 <?php bloginfo('name'); ?>
如上面所提到的,它显示了站点的名称,并且有管理员在选项 > 一般 默认的子面板上设置。
description <?php bloginfo('description'); ?>
This is called the "Tagline" for your blog which is usually some kind of descriptive sentence that says "My blog is about....". It is set by the administrator in the Options > General SubPanel.
描述 <?php bloginfo('description'); ?>
这个称作你的博客的"标签行",这个通常是一些描述性的语句,显示"我的博客是关于...."。它由管理员在选项 > 一般 子面板中设置。


url <?php bloginfo('url'); ?>
When you want to display the URL or website address for your WordPress site, you can use URL and it will show up. This also comes from the Options > General SubPanel.
url <?php bloginfo('url'); ?>
当你想为你的站点显示URL或者站点地址的时候,你可以使用URL,它就会显示出来。这也来自选项 > 一般 子面板。


admin_email <?php bloginfo('admin_email'); ?>
If you want to display the email of the administrator, you don't have to type it into the template files. By doing so, it may be open to email harvesters who use sophisticated software to come in and grab email addresses to use for spam. By using bloginfo('admin_email'), the email is displayed on the page for the viewers, but the actual email address is disguised from the harvesters. Nice, huh? The administrator's email address is set in the Options > General SubPanel.

;管理_邮件<?php bloginfo('admin_email'); ?>:如果你想显示管理员的邮件,你不用将它输进模板文件中。这样做,它可能对邮件 harvesters开放,他会使用复杂的软件来进入并且获得邮件地址用来发表垃圾信息。 通过使用bloginfo('admin_email'), 电子邮件在访客的页面上显示,但真正的地址对harvesters掩饰了。好, 哼? 管理员的电子邮件地址在 选项 > 一般 子面板中设置了。


version <?php bloginfo('version'); ?>
Sometimes you'd like to show off which version of WordPress you are using. The Themes that come with WordPress by default include this information in the footer template. It simply displays the version of WordPress your blog uses.

To show the WordPress version, the template tag would look like this:

<p>Powered by WordPress version <?php bloginfo('version'); ?></p>
Powered By WordPress version 模板:CurrentVersion
版本<?php bloginfo('version'); ?>
有时你想显示一下你正在使用的WordPress是哪一个版本。默认的WordPress主题在页脚模板中包含了这个信息。它只是显示了你的博客使用的WordPress版本。

To show the WordPress version, the template tag would look like this: 显示WordPress版本,模板标签看起来像:

<p>由WordPress 版本支持 <?php bloginfo('version'); ?></p>
由 WordPress 版本支持 模板:CurrentVersion


Notice that only the version number is generated by the version parameter, not the words "Powered by WordPress version". Those were written in before the tag so they would be visible on the web page.

注意只有版本数字由版本参数产生,而不是单词"由WordPress 版本支持"。这些单词写在标签的前面,因此可以在网页上看见它们。

To learn more about template tag parameters, see Anatomy of a Template Tag and How to Pass Tag Parameters.

学习更多的关于模板标签参数,看看 一个模板标签的解析怎样通过标签参数

How Do You Use Template Tags?

你怎样使用模板标签?

Going through the various template tags in the Wordpress:Template Tags menu on the Codex, you will see that many of them are very simple, like the bloginfo() template tag, but many look very complicated to use. Let's look at some examples of how they are used to help you understand the "language" of the template tag codes.

在Codex上模板标签菜单仔细看看不同的模板标签,你就会注意到它们中的许多非常的简单,像bloginfo() 模板标签,但许多看其来使用非常复杂。让我们来看一些例子,关于怎样使用它们来帮助你了解模板标签代码的"语言"。

As we saw in the bloginfo() template tag, all it took was one word to change the output of the tag. This word is called a parameter and it instructs the template tag to do or get something. In this case, the instruction is to get name which displays the site's name.

就如我们在bloginfo()模板标签中所见到的那样,它就用一个字来改变标签的产出。这个字叫做参数 而且它指示模板标签来 或者 得到一些东西。在这种情况下,指示说明是得到名称来显示站点的名称。

The template tag the_title() displays the title of the post, usually at the top of your post article. This tag gets the post title and displays it, by default, but it also has a do in the parameters which will help you change the look and presentation of the post title.

模板标签the_title()显示了文章的标题,通常在你的文章的上方。这个标签得到文章的标题并且显示了标题,默认情况下,但是它在参数中也有一个,可以帮助你改变文章标题的外观和显示。 By default, the tag looks like this:

默认情况下,标题看起来像这个:

<?php the_title(); ?>


<?php the_title(); ?>


And the results look something like this: 结果看起来像这个东西:

Using WordPress Makes Me Smile


Using WordPress Makes Me Smile


Let's say you want to put some kind of reference that highlights the title in some way, like a graphic or character entity like an arrow or bullet. Let's put a yen sign, ¥ ,the sign for Japanese money, in front of our title.

如果你想加上一些参考来以某种方式突出标题,像一个图形或者字符体像一个箭头符号或者bullet符号。让我们放进一个日元符号,&日元;,日本钱的标记,放到我们的标题的前面。

If you look carefully at the instructions for the tag the_title(), you will see that the parameters are:

<?php the_title('before', 'after', display); ?> 

如果你仔细看了标签the_title()的指示说明,你就会看到参数是:

<?php the_title('before', 'after', display); ?> 

We want the yen sign to be before the title, with a space after the yen sign and before the title, so let's add it to the parameters:

<?php the_title('&yen; '); ?> 

我们想将日元的标记放到标题的前面,在日元标记的后面和标题的前面都有一个空格,让我们吧它添加到参数上:

<?php the_title('&yen; '); ?> 

Which, when the page is generated, would look like this: 当网页产生了,这个看起来就像:

¥ Using WordPress Makes Me Smile


¥ Using WordPress Makes Me Smile


Now, let's take this a little further and put something after the post title. Let's say you want to encourage people to read so we'll add a little incentive arrow ( » ) to motivate them.

现在,我们再深入地看看这个,并且在文章标题的后面再添加一些东西。加入你想鼓励人们阅读,那么我们添加一个小的激励的箭头( » ) 来激发他们。

<?php the_title('&yen; ', ' &raquo;'); ?> 
<?php the_title('&yen; ', ' &raquo;'); ?> 


Notice, we added another space before the arrow to separate it from the post title when the page is generated for viewing. 注意,我们在箭头标记的前面有添加了一个空格,当网页可以浏览的时候,箭头标记与文章标题便分开了。

¥ Using WordPress Makes Me Smile »


¥ Using WordPress Makes Me Smile »


You can also style your title heading in many different ways. Here is another example using heading tags. 你也可以使用许多不同的方式来设计你的标题。下面是使用标题标签的另一个例子。

<h2><?php the_title('Post Title: '); ?></h2> 


<h2><?php the_title('Post Title: '); ?></h2> 


We've put the entire post title into an H2 heading and added the phrase "Post Title" to the beginning of the post title.

我们将整个文章的标题放到了一个H2标题中,并且在文章标题的开头添加短语"文章 标题"。

Post Title: Using WordPress Makes Me Smile


Post Title: Using WordPress Makes Me Smile


Note: Not all template tags take before and after arguments, though the_title does. Check the codex page for the specific tag you're using to see what arguments it accepts.

注: 虽然标题是这样的,并不是所有的 模板标签 接受论据之前或者之后。查看抄本页面,找你正使用的特别的标签,看看它支持什么样的论据。

Boolean Template Tags

布尔书学体系的模板标签

The above template tag example uses simple parameters separated from each other with quote marks and commas. Now consider examples of Boolean Template Tags that connect more than one parameter together using boolean math techniques. One common boolean expression uses the "and (&)" logic to connect the parameters.

以上的模板标签例子使用简单的参数,用引号和逗号将各自隔开。现在考虑一下Boolean 模板标签的例子,使用布尔数学体系技术将不止一个的参数连接起来。一个常见的布尔数学体系表述使用"and (&)"逻辑来连接参数。

The template tag wp_list_cats() is commonly found in the WordPress sidebar or menu template file. It lists the site's Categories. 模板标签wp_list_cats()通常能在WordPress工具条或者菜单模板文件中找到。它列出了站点的类别

<?php wp_list_cats(); ?>


<?php wp_list_cats(); ?>

By default, some of the template tags' parameters are: 默认情况下,一些模板标签的参数是:

  • all - Displays all of the Categories
  • 所有的 – 显示了所有的分类
  • sort_column - Sorts by Category ID
  • 类别_专栏 – 通过类别 ID的分类
  • sort_order - Sorts in ascending order
  • 类别_顺序 – 以升序的分类
  • list - Sets the Categories in an unordered list (<ul><li>)
  • 列表-将类别设置在一个无顺序的列表上

(<ul><li>)

  • optioncount - Does not display the count of posts within each Category
  • optioncount-在每个分类中,没有显示文章的数目
  • hide_empty - Based upon the first two parameters (optionall and all), does not display Categories without posts
  • 隐藏_空的-以前两个参数为基础(选择性的和所有的),不显示没有文章的分类
  • use_desc_for_title - Uses the Category description as the link title
  • use_desc_for_title –使用类别描述作为链接的标题
  • children - Shows the children (sub-Categories) of every Category listed
  • 孩子-显示每个列出的类别的孩子(子类别)

An example of this category list might be: 这个分类的一个例子可能是:


  • Stories About My Life
  • 关于我的生活的故事
  • Stories About My Family
  • 关于我的家庭的故事
  • Things I Want To Share
  • 我想分享的东西
    • About WordPress
    • 关于WordPress
    • About Writing
    • 关于写作
    • About Story Telling
    • 关于讲故事
  • Facts and Fiction About Life
  • 关于生活的事实和虚构小说
  • The indented list with "About WordPress", "About Writing", and "About Story Telling" are the children or sub-Categories of the parent Category "Things I Want To Share". These titles, by default, are not the actual titles of the Categories, they are the descriptions of the Category you set in the Administration > Manage > Categories panel.

    缩进的列表"关于 WordPress", "关于写", 和"关于讲故事" 是 分类 "我想分享的事情"的孩子或者子类别。默认情况下,这些标题并不是类别的真正的标题,它们是你在管理 > 管理 > 类别面板上设置的类别的描述



    If you would like to show the actual title of the Category, instead of the Category description, change the template tag to:

    <?php wp_list_cats('use_desc_for_title=0'); ?>

    如果你想显示分类的真正的标题,而不是分类的描述,将模板标签改为:

    <?php wp_list_cats('use_desc_for_title=0'); ?>

    The zero sets the parameter to false, turning off the use of the description as the title. Now the Category titles would appear: 零将参数设置为错误的,取消将描述用作标题。现在类别标题会显示:


    • My Life Stories
    • 我生活的故事
    • My Family
    • 我的家庭
    • Sharing
    • 分享
      • WordPress
      • WordPress
      • Writing
      • Story Telling
      • 将故事
    • Facts and Fiction
  • 事实与虚构

  • Let's say that you don't want the sub-Categories for "Sharing" to appear on your list. You would then add the parameter to not show the children, along with the parameter for showing only titles and not descriptions, with the boolean "and" using the ampersand ( & ). 如果你不想"分享的"子列表出现在你的列表上,你添加的参数就不会显示子列表,显示的参数只显示标题而不是表述,用布尔数学体系"并且"使用&记号标记( & )。

    <?php wp_list_cats('use_desc_for_title=0&children=0'); ?>


    <?php wp_list_cats('use_desc_for_title=0&children=0'); ?>


    Notice there are no spaces around the ampersand. All the parameters run together without any spaces or quote marks in between, just around the whole parameter. Now the Category titles would appear as:

    注意在&记号标记的周围没有空格。所有的参数在一起运行,其中没有任何的空格或者引号,只是在整个参数周围有。现在类别标题显示是:

    • My Life Stories
    • 我生活的故事
    • My Family
    • 我的家庭
    • Sharing
    • 分享
    • Facts and Fiction
  • 事实和虚构

  • As another example, if you want to display the Category links as the Category title, sort the list alphabetically by name, show the number of posts within each Category, and only show the children (sub-Categories) of Category ID number 3 ("Sharing"), the template tag would look like this:

    另一个例子,如果你想将类别链接显示为类别标题,将列表按名称的字母表顺序分类,显示每个类别中文章的数字,而且只显示类别ID数字3("分享")的孩子(子类别),模板标签看起来就像:

    <?php wp_list_cats('sort_column=name&sort_order=asc&optioncount=1&use_desc_for_title=0&child_of=3'); ?>


    <?php wp_list_cats('sort_column=name&sort_order=asc&optioncount=1&use_desc_for_title=0&child_of=3'); ?>


      • Story Telling (21)
      • 讲故事(21)
      • WordPress (23)
      • WordPress(23)
      • Writing (10)
  • 写(10)

  • Template Tags and The Loop

    模板标签和Loop

    Many of WordPress' template tags work within the WordPress Loop. This means that they are included in the template files as part of the php "loop" that generates the pages the viewer sees based upon the instructions inside of the Loop.

    许多的WordPress的模板标签在WordPress Loop中运行。这意味着,它们包含在模板文件里面,作为php "loop"的一部分,产生了网页,以Loop里面的指示为基础,访客可以看。 The WordPress Loop begins with: WordPress Loop的开头是:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>


    Template tags that work within the loop must be in the middle area here, before the ending section of the Loop below: 在Loop里面运行的模板标签,必须在这里的中间位置上,在下面的Loop的结尾部分之前:

    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>


    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    Template tags that need to be inside of the loop include the_content(), the_excerpt(), next_post(), and previous_post(). If the template tag you want to use doesn't have to be within the Loop, like wp_list_cats() and wp_list_pages(), then you can put it anywhere you like, for instance in the sidebar, header, or footer template files.

    需要包含在loop里的模板标签包括内容(), 摘录(), 下一篇文章(), 和 上一篇文章()。如果你想使用的模板标签不需要在loop里面,像wp_list_cats()wp_list_pages(),你可以将它放在任何你喜欢的位置,例如在工具条,标题上,或者页脚 t 模板文件


    Learning More About Template Tags

    学习更多关于模板标签

    This is just a tiny step into learning about the various powerful template tags WordPress uses to generate your website. You can learn more about the different template tags WordPress uses in the following articles and resources.

    这只是学习不同的强大的模板标签,WordPress用来制作你的站点的网页的微小的一步。在以下的文章和资源中,你可以学习更多的WordPress使用的不同的模板标签。


    Styling Your Template Tags

    设计你的模板标签


    External Resources

    外部资源