wordpress获取自定义字段的值:get_post_custom_values()


【描述】
用于获取当前文章的指定自定义字段的值,并以数组形式返回。
返回一个数组,包含某篇文章($post_id)某关键字($key)自定义fields的所有参数值。如果不存在这样的关键字,或者没有输入任何内容,不返回任何内容。
【用法】

  1. <?php get_post_custom_values($key, $post_id); ?>

【参数】
$key
(string) (必须) 你想要返回值得key.
Default: None
$post_id
(integer) (可选) 被查询的文章id.
Default: Current post

【示例】
默认用法
下面的例子将一个变数($mykey_values)设置为一个数组,包含关键字为my_key的当前文章的自定义fields的参数值。

  1. <?php
  2.  
  3. $mykey_values = get_post_custom_values('my_key');
  4. foreach ( $mykey_values as $key => $value ) {
  5. echo $key . " => " . $value . "<br />";
  6. }
  7.  
  8. ?>
  9. 0 => First value with "my_key" as its key
  10. 1 => Second value with "my_key" as its key
  11. 2 => Third value with "my_key" as its key
  12. etc...