Languages: English • Español • Nederlands • 日本語 Македонски • Português do Brasil • Русский • 中文(简体) • (Add your language)
文章作者可以在 WordPress 中为其文章指定自定义栏目。类似任意的附加信息通常叫做属性。这些属性可以是:
通过稍微多写的代码,还可以实现更多操作。比如使用属性来储存文章的到期时间。
属性的数据结构是键—值一一对应(key/value pair)。键就是属性元素的名称;值是会出现在信息所属的文章的属性列表中的信息。
键可以在一篇文章中使用多次。比如,当您在阅读两本不同的书的时候(没准一本是上班时间阅读的技术书籍,一本是家中阅读的小说),您可以创建“正在阅读”键,然后在同一文章中使用两次。
这是在您文章中,信息可能的展现形式:
Today's Mood: Jolly and Happy
|
| ||||
|
我们参考前面所提到的例子来操作. 我们将添加两个自定义栏目, 一个栏目称为 "正在阅读" 另一个栏目则称之为 "今天的心情". 下面我们演示如何使用自定义栏目显示信息。
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".
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.
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 The Loop in order to work. Many people add the_meta() template tag to the end of their post or in their Post Meta Data Section. Here is a basic example of using the tag:
<?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>
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.
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 {font-variant: small-caps; color: maroon; } .post-meta-key {color: green; font-weight: bold; font-size: 110%; }
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.
To register support for custom fields within a custom post type, simply list it in your 'supports' $args using 'custom-fields'.
'supports' => array('title','editor','thumbnail','custom-fields')
以下是使用更高级的方式获取自定义数据和自定义栏目的方法。
To fetch meta values use the get_post_meta() function:
get_post_meta($post_id, $key, $single);
The PostMeta information is stored in a new table, $wpdb->postmeta. This table has four fields:
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:
[ postid1 => [ key1 => [ val1, val2, ... ], key2 => [ val1, val2, ... ], ... ], postid2 => [ key1 => [ val1, val2, ... ], key2 => [ val1, val2, ... ], ... ], ... ]
So, if you wanted to fetch the "reading" values from post number 256, you use this PHP code:
// Fetch an array of values for what I'm reading: $readinglist = $post_meta_cache[256]['reading'];
Note: Don't forget that $readinglist will be an array, not a single value.
These functions are intended for use inside The Loop, and all return arrays.
This will output the resulting meta value (notice the addition of "echo"): <?php $key="mykey"; echo get_post_meta($post->ID, $key, true); ?>
At the time of this writing, there is only one template function.
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.