我们在使用WordPress编写文章的时候,通常可以看到一个“自定义栏目”,也经常看到别人的文章提到自定义字段,那么这个自定义栏目是用来做什么的,自定义字段(自定义域)又是什么东西?有什么作用?When we use WordPress to write articles, we can usually see a "custom column", and we often see other people’s articles mentioning custom fields, so what is this custom column for? Custom fields (custom Definition domain) What is it? what's the effect?
Introduction to WordPress custom fields WordPress custom fields are a supplement and extension to the WordPress Posts table. Generally speaking, WordPress provides the author, category, tag, time, etc. of the blog post, and then you can customize one according to your needs. Other information in the series. All these fields and values ​​are stored in the wp_post_meta data table in the database.
Many WordPress themes and plugins often use custom fields to extend functions. For example, the themes made by Advocate use custom fields to count the number of views of articles: WordPress custom fields work mechanism WordPress custom fields usually have two variables: key (key) and value (value ). For example, if you want to show your mood at the time you wrote the log, you can call the key "mood", obviously the value here is "happy" or "angry".
When we create a custom field for a log or page, the information we write will be stored in the log or page. In other words, the data of the custom field we created belongs to the current log or page, so we can display the custom field information related to the current log or page.
How to display the data of custom fields on the blog When we want to display the custom fields of the log or page, we have several methods. Here are two simple methods and some more advanced usages.
First, you can use the_meta() template function, it will automatically output an unordered list, as shown below: Key1: Value of “Key1″Key2: Value of “Key2″Key3: Value of “Key3″ The second function Yes: get_post_meta($post_id, $key, $single = true). This function can list custom fields according to your needs. For example, by using get_post_meta($post->ID, “Key2″, $single = true); you can return the value of the custom field “Key2″, and you can output it, such as “echo get_post_meta($post->ID, "Key2 ”, $single = true);”
Advanced usage of WordPress custom fields The following is an advanced usage of WordPress custom fields, you need to have a little PHP foundation.
So, if you want to use an emoticon picture to show the mood when writing a log, first make these emoticon pictures, and then name them Sad.gif, Mad.gif and Happy.gif, and the final code is as follows: <img src="/ smilies/<?php echo get_post_meta($post->ID, "mood", $single = true); ?>.gif" alt="mood" /> And if you refer to some books when writing a log, you want Tell readers to show which books you refer to when writing a log. This is also very easy. WordPress allows you to use multiple identical custom fields in a log, and then you can change "$single = true" to "$single = false" to "group" these keys. In other words, it will return an array, the code is as follows: <?php $books = get_post_meta($post->ID,"books", $single = false); ?><p>Reference books:</p>< ul><?php foreach( $books as $book) {echo "<li>".$book."</li>";}?></ul>WordPress custom fields extend WordPress WordPress custom fields greatly WordPress extends the functions of WordPress. Many themes are enhanced and defined by adding custom fields. Many plugins are based on WordPress custom fields. Flexible use of WordPress custom fields can turn WordPress into a powerful CMS system. By using custom fields, we can quickly add a lot of additional information to logs and pages, and quickly change the way the information is displayed without editing the logs.

WordPress 自定义字段 简介

WordPress 自定义字段是对 WordPress Posts 表的一种补充和扩展,一般来讲 WordPress 提供了博客日志的作者,分类,标签,时间等,然后你可以根据你的需要自定义出一系列的其他信息。所有的这些字段和值,都保存在数据库的 wp_post_meta 这个数据表里。

很多WordPress主题、插件都经常使用自定义字段来扩展功能。比如倡萌制作的主题都是使用了自定义字段来统计文章的浏览次数:

 

WordPress 自定义字段工作机制

WordPress 自定义字段通常有两个的变量:键 ( key ) 和值 ( value )。比如你想显示你写日志当时的心情,你可以把键叫做 “mood”(心情),显然在这里值就是 “高兴” 或者 “愤怒”。

当我们为某篇日志或者页面创建一个自定义字段的时候,我们所写的信息将会存储到该日志或者页面中。换句话说,我们所创建的自定义字段的的数据是属于当前的日志或者页面,所以我们能够显示当前日志或者页面相关的自定义字段信息。

如何在博客上显示自定义字段的数据

当我们想显示日志或者页面的自定义字段的时候,我们有几种方法。这里介绍两种简单的方法和一些更高级的用法。

首先,可以使用 the_meta() 这个模版函数,它会自动输出一个无序的列表,如下所示:

  • Key1: Value of “Key1″
  • Key2: Value of “Key2″
  • Key3: Value of “Key3″

第二个函数是:get_post_meta($post_id, $key, $single = true)。这个函数能够能按照自己的需求列出自定义字段。如通过使用 get_post_meta($post->ID, “Key2″, $single = true); 可以返回自定义字段 “Key2″ 的值,你可以输出它,如 “echo get_post_meta($post->ID, "Key2″, $single = true);

WordPress 自定义字段高级用法

下面是 WordPress 自定义字段的高级用法,需要你有一点 PHP 的基础。

所以,假如你想用一张表情图片显示写日志时候的心情,首先制作这些表情图片,然后把它们命名为 Sad.gif, Mad.gif and Happy.gif ,最后代码如下:

<img src="/smilies/<?php echo get_post_meta($post->ID, "mood", $single = true); ?>.gif" alt="mood" />

又假如你在写日志的时候参考一些书籍,你想告诉读者写日志时候显示你参考了那些书籍,这个也很易,WordPress 允许你在一篇日志中使用多个相同的自定义字段,然后可以通过把 "$single = true" 改成 "$single = false" 来“群组”这些键。换句话说,它会返回一个数组,代码如下:

<?php $books = get_post_meta($post->ID,"books", $single = false); ?>
<p>参考书籍:</p>
<ul>
<?php foreach( $books as $book ) {
    echo "<li>".$book."</li>";
}?>
</ul>

WordPress 自定义字段扩展了 WordPress

WordPress 自定义字段极大的扩展了 WordPress 的功能,很多主题都是通过增加自定义字段来增强和定义功能,很多插件都是基于 WordPress 自定义字段做的。灵活使用 WordPress 自定义字段可以把 WordPress 打造成强大的 CMS 系统,通过使用自定义字段,我们可以很快给日志和页面加上很多额外的信息,并且不用编辑日志就能很快改变信息显示方式。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。