WordPress: Using Custom Fields:修订间差异

来自站长百科
跳转至: 导航、​ 搜索
(新页面: __TOC__ WordPress has the ability to allow post authors to assign custom fields to a post. This arbitrary extra information is known as '''meta-data'''. This '''meta-data''' can include ...)
 
无编辑摘要
第1行: 第1行:
__TOC__
__TOC__
WordPress has the ability to allow post authors to assign custom fields to a post.  This arbitrary extra information is known as '''meta-data'''. This '''meta-data''' can include bits of information such as:
WordPress has the ability to allow post authors to assign custom fields to a post.  This arbitrary extra information is known as '''meta-data'''. This '''meta-data''' can include bits of information such as:
WordPress能够使文章作者给文章配置自定义区。这个任意的额外信息,称之为'''meta-data'''。这个'''meta-data'''可以包含以下的信息:


* '''Mood: Happy'''
* '''Mood: Happy'''
第6行: 第8行:
* '''Listening To: Rock Around the Clock'''
* '''Listening To: Rock Around the Clock'''
* '''Weather: Hot and humid'''
* '''Weather: Hot and humid'''
* '''情绪: 开心'''
* '''当前阅读: 灰姑娘'''
* '''聆听: 钟的摇摆'''
* '''天气: 闷热潮湿'''


With some extra coding, it is possible to achieve more complex actions, such as using the '''metadata''' to store an expiration date for a post.
With some extra coding, it is possible to achieve more complex actions, such as using the '''metadata''' to store an expiration date for a post.
拥有了一些额外的编码后,有可能归档更多的更加复杂的活动,例如使用'''metadata'''来为一篇文章储存一个有效期。


Meta-data is handled with '''key/value''' pairs.  The '''key''' is the name of the meta-data element.  The '''value''' is the information that will appear in the meta-data list on each individual post that the information is associated with.  
Meta-data is handled with '''key/value''' pairs.  The '''key''' is the name of the meta-data element.  The '''value''' is the information that will appear in the meta-data list on each individual post that the information is associated with.  
Meta-data是由'''关键词/值'''来处理的。'''关键词'''是meta-data元素的名称。'''值'''是每篇单独的与这个信息相关的文章上的meta-data列表的上显示的信息,


'''Keys''' can be used more than once per post. For example, if you were reading two different books (perhaps a technical book at work and a fiction at home), you could create a "reading" key and use it twice on the same post, once for each book.
'''Keys''' can be used more than once per post. For example, if you were reading two different books (perhaps a technical book at work and a fiction at home), you could create a "reading" key and use it twice on the same post, once for each book.
'''Keys'''可以在每篇文章中多次使用。例如,如果你正在阅读两本不同的书(可能工作时,读一本技术方面的书,在家的时候,读一本小说),你可以创建一个"阅读"关键词并且在同一篇文章中两次都使用这个关键词,为每本书都使用一次。


Here is an example of what this information might look like on your post:
Here is an example of what this information might look like on your post:
出现在你的文章中的信息可能是什么样的,下面是一个例子;


<div style="border:1px solid blue; width:50%; margin: 20px; padding:20px">'''Currently  Reading''': ''Calvin and Hobbes''
<div style="border:1px solid blue; width:50%; margin: 20px; padding:20px">'''Currently  Reading''': ''Calvin and Hobbes''
'''Today's Mood''': ''Jolly and Happy''
'''Today's Mood''': ''Jolly and Happy''
</div>
</div>
<div style="border:1px solid blue; width:50%; margin: 20px; padding:20px">'''当前阅读''': ''Calvin 和 Hobbes''
'''当日心情''': ''高兴愉快''
</div>


==Usage==
==Usage==
==用法==


Based upon our example above, let's put this into action.  We'll add two custom fields, one called "Currently Reading" and the other "Today's Mood". The following instructions will demonstrate how to add this information to a post using Custom Fields.
Based upon our example above, let's put this into action.  We'll add two custom fields, one called "Currently Reading" and the other "Today's Mood". The following instructions will demonstrate how to add this information to a post using Custom Fields.
根据上述的例子,让我们来实施这个。我们会添加两个自定义区,一个称作"当前阅读" 另一个称作"当日的心情"。以下的指示说明会指导你怎样使用自定义区来将这个信息添加到一篇文章中。


# After you have written your post, scroll down to the area titled '''Custom Fields'''.
# After you have written your post, scroll down to the area titled '''Custom Fields'''.
#你写了文章后,下拉到标题为'''自定义区'''的区域。
# To create a new '''Custom Field''' called "Currently Reading", enter the text "Currently Reading" (without the quotes) in the text entry field titled '''Key'''.
# To create a new '''Custom Field''' called "Currently Reading", enter the text "Currently Reading" (without the quotes) in the text entry field titled '''Key'''.
#要创建一个新的称之为"当前阅读"的'''自定义区''',在标题为'''关键词'''的文本输入区输入文本"当前阅读"(没有引号)。
# The newly created '''Key''' should now be assigned a '''Value''', which in our case is the name of the book currently being read, "Calvin and Hobbes". Type "Calvin and Hobbes" in the ''Value'' field, again without the quotes.
# The newly created '''Key''' should now be assigned a '''Value''', which in our case is the name of the book currently being read, "Calvin and Hobbes". Type "Calvin and Hobbes" in the ''Value'' field, again without the quotes.
#新创建的'''关键词'''现在应该有一个'''值''',在这个例子中,是当前阅读的书名,"Calvin 和Hobbes"。在''值'' 区输入"Calvin and Hobbes",仍然没有引号。
# Click '''Add Custom Field''' button to save this custom information for that post.
# Click '''Add Custom Field''' button to save this custom information for that post.
#点击'''添加自定义区'''按钮来将这篇文章的自定义信息保存下来。


To add your "Today's Mood", repeat the process and add "Today's Mood" to the '''key''' and a description of your mood in the '''value''' text boxes and click '''SAVE''' to save this information with the post.
To add your "Today's Mood", repeat the process and add "Today's Mood" to the '''key''' and a description of your mood in the '''value''' text boxes and click '''SAVE''' to save this information with the post.
要添加你的"当日的心情",重复这个过程,并且添加"当日的心情"到'''关键词''',同时在'''值'''文本框中添加你的心情的描述,并且点击'''保存'''来保存文章中的这个信息。


On your next post, you can add a new book and mood to your meta-data.  In the '''Custom Fields''' section, the '''Key''' will now feature a pull down list with the previously entered Custom Fields. Choose "Currently Reading" and then enter the new book you are reading in the '''value'''.  Click '''Add Custom Field''' and then repeat the process to add "Today's Mood".
On your next post, you can add a new book and mood to your meta-data.  In the '''Custom Fields''' section, the '''Key''' will now feature a pull down list with the previously entered Custom Fields. Choose "Currently Reading" and then enter the new book you are reading in the '''value'''.  Click '''Add Custom Field''' and then repeat the process to add "Today's Mood".
在你的下一篇文章中,你可以给你的meta-data添加一个本新书和心情。在'''自定义区'''部分,'''关键'''现在显示为一个下拉列表,这个列表上有先前输入的自定义范围。选择"当前阅读"然后输入你在'''值'''中阅读的新书。点击'''添加自定义范围'''然后重复这个过程来添加"当日的心情"。


You only need to create a new "KEY" '''once''', after which you can assign a value to that key for every post, if you so desire. You can also assign more than one ''Value'' to a key, for a post. This will come in handy for people who read more than one book at a time.
You only need to create a new "KEY" '''once''', after which you can assign a value to that key for every post, if you so desire. You can also assign more than one ''Value'' to a key, for a post. This will come in handy for people who read more than one book at a time.
你'''一次'''只要创建一个新的"关键词",创建好了之后,如果你想的话,你可以为每篇文章的关键词分配一个值。你可以为一篇文章的一个关键词分配几个''值''。对于那些一次阅读几本书的人们,这个功能迟早会有用的。


==Displaying Custom Fields==
==Displaying Custom Fields==
==显示自定义范围==


With a Custom Field added to the post, it's time to display your books and mood to the world.  To display the Custom Fields for each post, use the <tt>[[WordPress:Template_Tags/the_meta|the_meta()</tt>]] template tag. The tag must be put within [[WordPress:The Loop]] in order to work.  Many people add <tt>the_meta()</tt> template tag to the end of their post or in their [[WordPress:Post Meta Data Section]]. Here is a basic example of using the tag:
With a Custom Field added to the post, it's time to display your books and mood to the world.  To display the Custom Fields for each post, use the <tt>[[WordPress:Template_Tags/the_meta|the_meta()</tt>]] template tag. The tag must be put within [[WordPress:The Loop]] in order to work.  Many people add <tt>the_meta()</tt> template tag to the end of their post or in their [[WordPress:Post Meta Data Section]]. Here is a basic example of using the tag:
给文章添加了一个自定义范围之后,就是向世界显示你的书本和心情的时候了。要为每篇文章显示自定义范围,使用<tt>[[WordPress:Template_Tags/the_meta|the_meta()</tt>]]模板标签。标签必须放入[[WordPress:The Loop|The Loop]]中才可以运行。许多人将<tt>the_meta()</tt>模板标签添加在他们的文章的最后部分,或者添加在他们的[[WordPress:Post Meta Data Section|文章 Meta Data部分]]。下面是使用标签的一个基本的例子:


  <?php the_meta(); ?>
  <?php the_meta(); ?>
<?php the_meta(); ?>


It might look like this in the source code:
It might look like this in the source code:
在源代码中,这个可能看起来像:
<pre>&lt;ul class='post-meta'&gt;
&lt;li&gt;&lt;span class='post-meta-key'&gt;Curently Reading:&lt;/span&gt; Calvin and Hobbes&lt;/li&gt;
&lt;li&gt;&lt;span class='post-meta-key'&gt;Today's Mood:&lt;/span&gt; Jolly and Happy&lt;/li&gt;
&lt;/ul&gt;</pre>


<pre>&lt;ul class='post-meta'&gt;
<pre>&lt;ul class='post-meta'&gt;
第47行: 第107行:
&lt;/ul&gt;</pre>
&lt;/ul&gt;</pre>


The template tag automatically puts the entire meta-data into a CSS style called <tt>post-meta</tt>. The '''key''' is in a <tt>span</tt> called <tt>post-meta-key</tt> so you can style it in your style sheet.  All of this is showcased in an unordered list.
 
 
The template tag automatically puts the entire meta-data into a CSS style called <tt>post-meta</tt>. The '''key''' is in a <tt>span</tt> called <tt>post-meta-key</tt> so you can style it in your style sheet.  All of this is showcased in an unordered list.  
模板标签自动地将这个的meta-data放到一个CSS样式中,称为<tt>post-meta</tt>。'''关键词'''是在一个<tt>span</tt>中,称为<tt>post-meta-key</tt>,这样你可以在你的样式表中将它设计。所有的这些内容都在一个无序的列表中得到了显示。


To customize the look of the post-meta list, change the characteristics in your style sheet.  For instance, let's add some style to our example from the top.  The style sheet elements would look like this:
To customize the look of the post-meta list, change the characteristics in your style sheet.  For instance, let's add some style to our example from the top.  The style sheet elements would look like this:
要自定义post-meta列表的外观,就要改变你的样式表的特征。例如,,让我们给上面的例子添加一些样式。样式表元素看起来像:


<pre>.post-meta {font-variant: small-caps; color: maroon; }
<pre>.post-meta {font-variant: small-caps; color: maroon; }
.post-meta-key {color: green; font-weight: bold; font-size: 110%; }</pre>
.post-meta-key {color: green; font-weight: bold; font-size: 110%; }</pre>
<pre>.post-meta {font-variant: small-caps; color: maroon; }
.post-meta-key {颜色: 绿色; font-weight: 粗体; 字体大小: 110%; }</pre>
<div style="border:1px solid blue; width:50%; margin: 20px; padding:20px">


<div style="border:1px solid blue; width:50%; margin: 20px; padding:20px">
<div style="border:1px solid blue; width:50%; margin: 20px; padding:20px">
* <span style="font-size:110%; color: green; font-weight:bold; font-variant: small-caps">Currently  Reading</span>: <span style="font-style:italic; color: maroon; font-variant: small-caps">Calvin and Hobbes</span>
* <span style="font-size:110%; color: green; font-weight:bold; font-variant: small-caps">Currently  Reading</span>: <span style="font-style:italic; color: maroon; font-variant: small-caps">Calvin and Hobbes</span>
* <span style="font-size:110%; 颜色: 绿色; font-weight:黑体; font-variant: small-caps">Currently  阅读</span>: <span style="font-style:italic; 颜色: 栗色; font-variant: small-caps">Calvin 和 Hobbes</span>
* <span style="font-size:110%; color: green; font-weight:bold; font-variant: small-caps">Today's Mood</span>: <span style="font-style:italic; color: maroon; font-variant: small-caps">Jolly and Happy</span>
* <span style="font-size:110%; color: green; font-weight:bold; font-variant: small-caps">Today's Mood</span>: <span style="font-style:italic; color: maroon; font-variant: small-caps">Jolly and Happy</span>
</div>
</div>
* <span style="font-size:110%; 颜色: 绿色; font-weight:粗体; font-variant: small-caps">Today's 心情</span>: <span style="font-style:italic; 颜色: 栗色; font-variant: small-caps">开心愉快</span>
</div>


There are also many WordPress Plugins in the [http://wordpress.org/extend/plugins/ Official WordPress Plugin Directory] that add some nice features to the job of displaying meta tags. A search for [http://www.google.com/search?hl=en&q=custom+fields+plugin+wordpress&btnG=Google+Search Custom Field plugins at Google] should help you find even more.
There are also many WordPress Plugins in the [http://wordpress.org/extend/plugins/ Official WordPress Plugin Directory] that add some nice features to the job of displaying meta tags. A search for [http://www.google.com/search?hl=en&q=custom+fields+plugin+wordpress&btnG=Google+Search Custom Field plugins at Google] should help you find even more.
[http://wordpress.org/extend/plugins/ 官方 WordPress 插件目录]中有许多WordPress插件,给显示meta标签增添了一些好的功能。搜索[http://www.google.com/search?hl=en&q=custom+fields+plugin+wordpress&btnG=Google+Search Google上的自定义范围插件]会帮助你找到更多的插件。


==Advanced Techniques for Custom Fields==
==Advanced Techniques for Custom Fields==
==自定义区的高级技术==


The following are more advanced techniques for getting and customizing meta-data and custom fields.
The following are more advanced techniques for getting and customizing meta-data and custom fields.
下面是更多的关于得到并且自定义meta-data和自定义区的高级技术。


===Getting Custom Fields===
===Getting Custom Fields===
===得到自定义区===
To fetch meta values use the <tt>get_post_meta()</tt> function:
To fetch meta values use the <tt>get_post_meta()</tt> function:
使用<tt>get_post_meta()</tt>函数,能够得到meta值:


   get_post_meta($post_id, $key, $single);
   get_post_meta($post_id, $key, $single);
get_post_meta($post_id, $key, $single);


*<tt>$post_id</tt> is the ID of the post you want the meta values for. Use <tt>$post->ID</tt> to get a post's ID.
*<tt>$post_id</tt> is the ID of the post you want the meta values for. Use <tt>$post->ID</tt> to get a post's ID.
*<tt>$post_id</tt>是mata值想指的文章的ID。使用<tt>$post->ID</tt>来得到文章的ID。
*<tt>$key</tt> is a string containing the name of the meta value you want.
*<tt>$key</tt> is a string containing the name of the meta value you want.
*<tt>$key</tt>是一个字符串,包含了你想要的meta 值的名称。
*<tt>$single</tt> can either be <tt>true</tt> or <tt>false</tt>. If set to true then the function will return a single result, as a '''string'''. If false, or not set, then the function returns an '''array''' of the custom fields.
*<tt>$single</tt> can either be <tt>true</tt> or <tt>false</tt>. If set to true then the function will return a single result, as a '''string'''. If false, or not set, then the function returns an '''array''' of the custom fields.
*<tt>$single</tt>可以是<tt>正确的</tt> 或者<tt>错误的</tt>。如果设置为正确的,函数会返回一个单一的结果,作为一个'''字符串'''。如果设置为错误的,或者没有设置,那么函数会返回一个自定义范围的'''数组'''。


====Implementation Details====
====Implementation Details====
====执行详细信息====
The PostMeta information is stored in a new table, <tt>$wpdb->postmeta</tt>. This table has four fields:
The PostMeta information is stored in a new table, <tt>$wpdb->postmeta</tt>. This table has four fields:
PostMeta信息储存在一个新的表格中,<tt>$wpdb->postmeta</tt>。这个表格有四个区:


:<tt>meta_id</tt>: A unique id for each entry<br />
:<tt>meta_id</tt>: A unique id for each entry<br />
第81行: 第190行:
:<tt>meta_key</tt>: The name of the 'key'<br />
:<tt>meta_key</tt>: The name of the 'key'<br />
:<tt>meta_value</tt>: The value associated with the key<br />
:<tt>meta_value</tt>: The value associated with the key<br />
:<tt>meta_id</tt>: 每篇文章的一个唯一的id <br />
:<tt>post_id</tt>: 这个metadata文章的ID <br />
:<tt>meta_key</tt>: '关键词'的名称<br />
:<tt>meta_value</tt>: 与关键词相关的值<br />


The values from this table are pulled into a structured multi-dimensional array called <tt>$post_meta_cache</tt>, just after the <tt>$posts</tt> array is fetched in <tt>wp-blog-header.php</tt>. This variable will only contain values for the list of posts fetched for the current page build. The structure of the array will look something like this:
The values from this table are pulled into a structured multi-dimensional array called <tt>$post_meta_cache</tt>, just after the <tt>$posts</tt> array is fetched in <tt>wp-blog-header.php</tt>. This variable will only contain values for the list of posts fetched for the current page build. The structure of the array will look something like this:
这个表格中的值被放进了一个称为<tt>$post_meta_cache</tt>的结构多维数组,在<tt>$posts</tt>数组被引进<tt>wp-blog-header.php</tt>之后。这个变数只包括那些寻求当前创建的网页的文章的列表的值。数组的结果可能看起来像:


   [
   [
第92行: 第210行:
       ],
       ],
     postid2 => [ ... ],
     postid2 => [ ... ],
    ...
  ]
[
    文章id1 => [
      [
        关键词1 => [val1, val2, ...],
        关键词2 => [val1, val2, ...],
        ...
      ],
    文章id2 => [ ... ],
     ...
     ...
   ]
   ]


So, if you wanted to fetch the "reading" values from post number 256, you use this PHP code:
So, if you wanted to fetch the "reading" values from post number 256, you use this PHP code:
因此,如果你想要从文章号256中提取"阅读"值,你使用这个PHP编码:


   // Fetch an array of values for what I'm reading:
   // Fetch an array of values for what I'm reading:
   $readinglist = $post_meta_cache[256]['reading'];
   $readinglist = $post_meta_cache[256]['reading'];
// 为我所阅读的内容找到一组值:
  $readinglist = $post_meta_cache[256]['阅读'];


:Don't forget that <tt>$readinglist</tt> will be an array, not a single value.
:Don't forget that <tt>$readinglist</tt> will be an array, not a single value.
:不要忘了<tt>$readinglist</tt>会是一个数组,而不是一个单一的值。
:As of WordPress 2.1, $post_meta_cache isn't populated anymore. Get the meta values through the functions mentioned below
:As of WordPress 2.1, $post_meta_cache isn't populated anymore. Get the meta values through the functions mentioned below
:从WordPress2.1起,$post_meta_cache上不再有meta值。从下面提到的函数中得到meta值


===PostMeta Functions===
===PostMeta Functions===
===PostMeta 函数===


====Internal Functions====
====Internal Functions====
====内部函数====


These functions are intended for use inside [[WordPress:The Loop]], and all return arrays.
These functions are intended for use inside [[WordPress:The Loop]], and all return arrays.
这些函数将在[[WordPress:The Loop|The Loop]]内部使用,而且这些函数都返回数组。


;<tt>[[WordPress:Function Reference/get post custom|get_post_custom()]]</tt>: Get all key/value data for the current post.
;<tt>[[WordPress:Function Reference/get post custom|get_post_custom()]]</tt>: Get all key/value data for the current post.
;<tt>[[WordPress:Function Reference/get post custom|得到_文章_自定义()]]</tt>:得到当前文章的所有的关键词/值数据。
;<tt>[[WordPress:Function Reference/get post custom keys|get_post_custom_keys()]]</tt>: Get a list of all key names for the current post.
;<tt>[[WordPress:Function Reference/get post custom keys|get_post_custom_keys()]]</tt>: Get a list of all key names for the current post.
;<tt>[[WordPress:Function Reference/get post custom keys|得到_文章_自定义_关键词()]]</tt>:得到当前文章的所有关键词名称的列表。
;<tt>[[WordPress:Function Reference/get post custom values|get_post_custom_values($key)]]</tt>: Get the list of values for a particular key on the current post.
;<tt>[[WordPress:Function Reference/get post custom values|get_post_custom_values($key)]]</tt>: Get the list of values for a particular key on the current post.
;<tt>[[WordPress:Function Reference/get post custom values|得到_文章_自定义_值($key)]]</tt>:得到当前文章的一个特别的关键词的值的列表。
;<tt>get_post_meta($post_id, $key, $single = false)</tt>: In WP 1.5 and beyond, this function returns the meta information without cache problems. The function requires the post id, the key, and if <tt>$single</tt> is set to TRUE, it returns only the first result (NOT as an array) for PHP use.
;<tt>get_post_meta($post_id, $key, $single = false)</tt>: In WP 1.5 and beyond, this function returns the meta information without cache problems. The function requires the post id, the key, and if <tt>$single</tt> is set to TRUE, it returns only the first result (NOT as an array) for PHP use.
;<tt>get_post_meta($post_id, $key, $single = false)</tt>:在WP1.5以及更高的版本中,这个函数返回了meta信息,但是不会产生高速缓存问题。函数需要文章id,关键词,如果<tt>$single</tt>设置为真的,它只会返回PHP使用的第一个结果(不是作为一个数组)。


<pre>This will output the resulting meta value (notice the addition of "echo"):
<pre>This will output the resulting meta value (notice the addition of "echo"):
<pre>这个会产出作为结果的meta值 (注意添加的"echo"):
<?php $key="mykey"; echo get_post_meta($post->ID, $key, true); ?></pre>
<?php $key="mykey"; echo get_post_meta($post->ID, $key, true); ?></pre>
<?php $key="mykey"; echo get_post_meta($post->ID, $key, true); ?></pre>


====Template Functions====
====Template Functions====
====模板函数====


At the time of this writing, there is only one template function.
At the time of this writing, there is only one template function.
写这个的时候,只有一个模板函数。


;<tt>[[WordPress:Template Tags/the_meta|the_meta()]]</tt>:Echoes an unordered list containing the current post's meta-data with a class for the UL as ''post-meta'' and the LI as ''post-meta-key''.
;<tt>[[WordPress:Template Tags/the_meta|the_meta()]]</tt>:Echoes an unordered list containing the current post's meta-data with a class for the UL as ''post-meta'' and the LI as ''post-meta-key''.
;<tt>[[WordPress:Template Tags/the_meta|the_meta()]]</tt>:Echoes一个无序的列表,这个列表包含有当前的文章的meta-data,有一类是关于UL的''post-meta''和LI的''post-meta-key''。


We expect that independent developers will come up with many interesting uses for post meta-data in the form of plugins. The <tt>the_meta()</tt> template function is just an extremely basic example.
We expect that independent developers will come up with many interesting uses for post meta-data in the form of plugins. The <tt>the_meta()</tt> template function is just an extremely basic example.
我们期望独立的开发者能够提出许多插件形式的文章meta-data的有趣的用法。<tt>the_meta()</tt>模板函数只是一个特别的基本的例子。


At this time, you can only add and delete entries. The ability to modify existing entries will be implemented later.
At this time, you can only add and delete entries. The ability to modify existing entries will be implemented later.
这时,你只能够添加和删除文章。以后会给你更改当前文章的权限。

2008年6月12日 (四) 17:43的版本

WordPress has the ability to allow post authors to assign custom fields to a post. This arbitrary extra information is known as meta-data. This meta-data can include bits of information such as:

WordPress能够使文章作者给文章配置自定义区。这个任意的额外信息,称之为meta-data。这个meta-data可以包含以下的信息:

  • Mood: Happy
  • Currently Reading: Cinderella
  • Listening To: Rock Around the Clock
  • Weather: Hot and humid


  • 情绪: 开心
  • 当前阅读: 灰姑娘
  • 聆听: 钟的摇摆
  • 天气: 闷热潮湿


With some extra coding, it is possible to achieve more complex actions, such as using the metadata to store an expiration date for a post.

拥有了一些额外的编码后,有可能归档更多的更加复杂的活动,例如使用metadata来为一篇文章储存一个有效期。

Meta-data is handled with key/value pairs. The key is the name of the meta-data element. The value is the information that will appear in the meta-data list on each individual post that the information is associated with.

Meta-data是由关键词/值来处理的。关键词是meta-data元素的名称。是每篇单独的与这个信息相关的文章上的meta-data列表的上显示的信息,

Keys can be used more than once per post. For example, if you were reading two different books (perhaps a technical book at work and a fiction at home), you could create a "reading" key and use it twice on the same post, once for each book.

Keys可以在每篇文章中多次使用。例如,如果你正在阅读两本不同的书(可能工作时,读一本技术方面的书,在家的时候,读一本小说),你可以创建一个"阅读"关键词并且在同一篇文章中两次都使用这个关键词,为每本书都使用一次。

Here is an example of what this information might look like on your post:

出现在你的文章中的信息可能是什么样的,下面是一个例子;

Currently Reading: Calvin and Hobbes

Today's Mood: Jolly and Happy


当前阅读: Calvin 和 Hobbes

当日心情: 高兴愉快


Usage

用法

Based upon our example above, let's put this into action. We'll add two custom fields, one called "Currently Reading" and the other "Today's Mood". The following instructions will demonstrate how to add this information to a post using Custom Fields.

根据上述的例子,让我们来实施这个。我们会添加两个自定义区,一个称作"当前阅读" 另一个称作"当日的心情"。以下的指示说明会指导你怎样使用自定义区来将这个信息添加到一篇文章中。

  1. After you have written your post, scroll down to the area titled Custom Fields.
  1. 你写了文章后,下拉到标题为自定义区的区域。
  1. To create a new Custom Field called "Currently Reading", enter the text "Currently Reading" (without the quotes) in the text entry field titled Key.
  1. 要创建一个新的称之为"当前阅读"的自定义区,在标题为关键词的文本输入区输入文本"当前阅读"(没有引号)。
  1. The newly created Key should now be assigned a Value, which in our case is the name of the book currently being read, "Calvin and Hobbes". Type "Calvin and Hobbes" in the Value field, again without the quotes.
  1. 新创建的关键词现在应该有一个,在这个例子中,是当前阅读的书名,"Calvin 和Hobbes"。在 区输入"Calvin and Hobbes",仍然没有引号。
  1. Click Add Custom Field button to save this custom information for that post.
  1. 点击添加自定义区按钮来将这篇文章的自定义信息保存下来。

To add your "Today's Mood", repeat the process and add "Today's Mood" to the key and a description of your mood in the value text boxes and click SAVE to save this information with the post.

要添加你的"当日的心情",重复这个过程,并且添加"当日的心情"到关键词,同时在文本框中添加你的心情的描述,并且点击保存来保存文章中的这个信息。

On your next post, you can add a new book and mood to your meta-data. In the Custom Fields section, the Key will now feature a pull down list with the previously entered Custom Fields. Choose "Currently Reading" and then enter the new book you are reading in the value. Click Add Custom Field and then repeat the process to add "Today's Mood". 在你的下一篇文章中,你可以给你的meta-data添加一个本新书和心情。在自定义区部分,关键现在显示为一个下拉列表,这个列表上有先前输入的自定义范围。选择"当前阅读"然后输入你在中阅读的新书。点击添加自定义范围然后重复这个过程来添加"当日的心情"。


You only need to create a new "KEY" once, after which you can assign a value to that key for every post, if you so desire. You can also assign more than one Value to a key, for a post. This will come in handy for people who read more than one book at a time.

一次只要创建一个新的"关键词",创建好了之后,如果你想的话,你可以为每篇文章的关键词分配一个值。你可以为一篇文章的一个关键词分配几个。对于那些一次阅读几本书的人们,这个功能迟早会有用的。

Displaying Custom Fields

显示自定义范围

With a Custom Field added to the post, it's time to display your books and mood to the world. To display the Custom Fields for each post, use the the_meta() template tag. The tag must be put within WordPress:The Loop in order to work. Many people add the_meta() template tag to the end of their post or in their WordPress:Post Meta Data Section. Here is a basic example of using the tag:

给文章添加了一个自定义范围之后,就是向世界显示你的书本和心情的时候了。要为每篇文章显示自定义范围,使用the_meta()模板标签。标签必须放入The Loop中才可以运行。许多人将the_meta()模板标签添加在他们的文章的最后部分,或者添加在他们的文章 Meta Data部分。下面是使用标签的一个基本的例子:

<?php the_meta(); ?>

<?php the_meta(); ?>

It might look like this in the source code: 在源代码中,这个可能看起来像:


<ul class='post-meta'>
<li><span class='post-meta-key'>Curently Reading:</span> Calvin and Hobbes</li>
<li><span class='post-meta-key'>Today's Mood:</span> Jolly and Happy</li>
</ul>


<ul class='post-meta'>
<li><span class='post-meta-key'>Curently Reading:</span> Calvin and Hobbes</li>
<li><span class='post-meta-key'>Today's Mood:</span> Jolly and Happy</li>
</ul>


The template tag automatically puts the entire meta-data into a CSS style called post-meta. The key is in a span called post-meta-key so you can style it in your style sheet. All of this is showcased in an unordered list. 模板标签自动地将这个的meta-data放到一个CSS样式中,称为post-meta关键词是在一个span中,称为post-meta-key,这样你可以在你的样式表中将它设计。所有的这些内容都在一个无序的列表中得到了显示。

To customize the look of the post-meta list, change the characteristics in your style sheet. For instance, let's add some style to our example from the top. The style sheet elements would look like this:

要自定义post-meta列表的外观,就要改变你的样式表的特征。例如,,让我们给上面的例子添加一些样式。样式表元素看起来像:

.post-meta {font-variant: small-caps; color: maroon; }
.post-meta-key {color: green; font-weight: bold; font-size: 110%; }


.post-meta {font-variant: small-caps; color: maroon; }
.post-meta-key {颜色: 绿色; font-weight: 粗体; 字体大小: 110%; }


  • Currently Reading: Calvin and Hobbes
  • Currently 阅读: Calvin 和 Hobbes


  • Today's Mood: Jolly and Happy
  • Today's 心情: 开心愉快


There are also many WordPress Plugins in the Official WordPress Plugin Directory that add some nice features to the job of displaying meta tags. A search for Custom Field plugins at Google should help you find even more.

官方 WordPress 插件目录中有许多WordPress插件,给显示meta标签增添了一些好的功能。搜索Google上的自定义范围插件会帮助你找到更多的插件。

Advanced Techniques for Custom Fields

自定义区的高级技术

The following are more advanced techniques for getting and customizing meta-data and custom fields.

下面是更多的关于得到并且自定义meta-data和自定义区的高级技术。

Getting Custom Fields

得到自定义区

To fetch meta values use the get_post_meta() function:

使用get_post_meta()函数,能够得到meta值:

 get_post_meta($post_id, $key, $single);

get_post_meta($post_id, $key, $single);


  • $post_id is the ID of the post you want the meta values for. Use $post->ID to get a post's ID.
  • $post_id是mata值想指的文章的ID。使用$post->ID来得到文章的ID。
  • $key is a string containing the name of the meta value you want.
  • $key是一个字符串,包含了你想要的meta 值的名称。
  • $single can either be true or false. If set to true then the function will return a single result, as a string. If false, or not set, then the function returns an array of the custom fields.
  • $single可以是正确的 或者错误的。如果设置为正确的,函数会返回一个单一的结果,作为一个字符串。如果设置为错误的,或者没有设置,那么函数会返回一个自定义范围的数组

Implementation Details

执行详细信息

The PostMeta information is stored in a new table, $wpdb->postmeta. This table has four fields:

PostMeta信息储存在一个新的表格中,$wpdb->postmeta。这个表格有四个区:

meta_id: A unique id for each entry
post_id: The ID of the post for this metadata
meta_key: The name of the 'key'
meta_value: The value associated with the key


meta_id: 每篇文章的一个唯一的id
post_id: 这个metadata文章的ID
meta_key: '关键词'的名称
meta_value: 与关键词相关的值


The values from this table are pulled into a structured multi-dimensional array called $post_meta_cache, just after the $posts array is fetched in wp-blog-header.php. This variable will only contain values for the list of posts fetched for the current page build. The structure of the array will look something like this:

这个表格中的值被放进了一个称为$post_meta_cache的结构多维数组,在$posts数组被引进wp-blog-header.php之后。这个变数只包括那些寻求当前创建的网页的文章的列表的值。数组的结果可能看起来像:

 [
   postid1 => [
     [
       key1 => [val1, val2, ...],
       key2 => [val1, val2, ...],
       ...
     ],
   postid2 => [ ... ],
   ...
 ]


[

   文章id1 => [
     [
       关键词1 => [val1, val2, ...],
       关键词2 => [val1, val2, ...],
       ...
     ],
   文章id2 => [ ... ],
   ...
 ]

So, if you wanted to fetch the "reading" values from post number 256, you use this PHP code:

因此,如果你想要从文章号256中提取"阅读"值,你使用这个PHP编码:

 // Fetch an array of values for what I'm reading:
 $readinglist = $post_meta_cache[256]['reading'];


// 为我所阅读的内容找到一组值:

 $readinglist = $post_meta_cache[256]['阅读'];
Don't forget that $readinglist will be an array, not a single value.

:不要忘了$readinglist会是一个数组,而不是一个单一的值。

As of WordPress 2.1, $post_meta_cache isn't populated anymore. Get the meta values through the functions mentioned below

:从WordPress2.1起,$post_meta_cache上不再有meta值。从下面提到的函数中得到meta值

PostMeta Functions

PostMeta 函数

Internal Functions

内部函数

These functions are intended for use inside WordPress:The Loop, and all return arrays.

这些函数将在The Loop内部使用,而且这些函数都返回数组。

get_post_custom()
Get all key/value data for the current post.
得到_文章_自定义()
得到当前文章的所有的关键词/值数据。
get_post_custom_keys()
Get a list of all key names for the current post.
得到_文章_自定义_关键词()
得到当前文章的所有关键词名称的列表。
get_post_custom_values($key)
Get the list of values for a particular key on the current post.
得到_文章_自定义_值($key)
得到当前文章的一个特别的关键词的值的列表。
get_post_meta($post_id, $key, $single = false)
In WP 1.5 and beyond, this function returns the meta information without cache problems. The function requires the post id, the key, and if $single is set to TRUE, it returns only the first result (NOT as an array) for PHP use.
get_post_meta($post_id, $key, $single = false)
在WP1.5以及更高的版本中,这个函数返回了meta信息,但是不会产生高速缓存问题。函数需要文章id,关键词,如果$single设置为真的,它只会返回PHP使用的第一个结果(不是作为一个数组)。
This will output the resulting meta value (notice the addition of "echo"):

<pre>这个会产出作为结果的meta值 (注意添加的"echo"):


<?php $key="mykey"; echo get_post_meta($post->ID, $key, true); ?>


<?php $key="mykey"; echo get_post_meta($post->ID, $key, true); ?>

Template Functions

模板函数

At the time of this writing, there is only one template function.

写这个的时候,只有一个模板函数。

the_meta()
Echoes an unordered list containing the current post's meta-data with a class for the UL as post-meta and the LI as post-meta-key.
the_meta()
Echoes一个无序的列表,这个列表包含有当前的文章的meta-data,有一类是关于UL的post-meta和LI的post-meta-key

We expect that independent developers will come up with many interesting uses for post meta-data in the form of plugins. The the_meta() template function is just an extremely basic example.

我们期望独立的开发者能够提出许多插件形式的文章meta-data的有趣的用法。the_meta()模板函数只是一个特别的基本的例子。

At this time, you can only add and delete entries. The ability to modify existing entries will be implemented later.

这时,你只能够添加和删除文章。以后会给你更改当前文章的权限。