如何读取页面的自定义字段的值?

时间:2010-09-22 作者:Hinek

创建自定义页面模板时,如何获取显示页面的自定义字段的值?

2 个回复
最合适的回答,由SO网友:sorich87 整理而成

Use the get_post_meta function.

SO网友:Chris_O

WordPress自定义字段包含键和值。两者兼得<?php the_meta(); ?> 将返回

<ul class=\'post-meta\'>
<li><span class=\'post-meta-key\'>your_key:</span> your_value</li>
</ul>

To return a value stored for a specific key use:

<?php get_post_custom_values(\'my_key\', $post_id); ?>**

If a post contained more than 1 value for the same key you can use a foreach loop to output all of them i an array:

<?php    
  $mykey_values = get_post_custom_values(\'my_key\');
  foreach ( $mykey_values as $key => $value ) {
    echo "$key  => $value <br />"; 
  }  
?>

This will return:

0 => First value 
1 => Second value
2 => Third value 
To return an array of keys within all custom fields use:

<?php get_post_custom_keys($post_id); ?> 

Adding custom fields to a post:


alt text

结束

相关推荐