WordPress函数get_post_custom()通过id获取文章/页面所有的自定义字段(post meta)信息

描述:

通过id获取文章/页面所有的自定义字段(post meta)信息

用法:

 <?php get_post_custom($post_id); ?> 

参数:

$post_id

(integer) (可选) 将检索其自定义字段的post ID。

默认值: 当前文章

示例:

<?php
  $custom_fields = get_post_custom(72);
  $my_custom_field = $custom_fields['my_custom_field'];
  foreach ( $my_custom_field as $key => $value ) {
    echo $key . " => " . $value . "<br />";
  }
?>

源文件:

function get_post_custom( $post_id = 0 ) {
    $post_id = absint( $post_id );
    if ( ! $post_id ) {
        $post_id = get_the_ID();
    }
 
    return get_post_meta( $post_id );
}