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

目录

描述

译文

返回一个数组,数组中含有特定文章或页面的所有自定义字段的关键字。get_post_custom_keys() and get_post_custom_values() 。

原文

Returns an array containing the keys of all custom fields of a particular post or page. See also get_post_custom() and get_post_custom_values()

get_post_custom_keys() 描述

用法

 <?php get_post_custom_keys($post_id); ?> 

get_post_custom_keys() 用法

参数

$post_id

(integer) (可选) The post ID whose custom field keys will be retrieved.

默认值: Current post

get_post_custom_keys() 参数

示例

Default Usage

The following example will set a variable ($custom_field_keys) as an array containing the keys of all custom fields in the current post, and then print it. Note: the if test excludes values for WordPress internally maintained custom keys such as _edit_last and _edit_lock.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php

$custom_field_keys = get_post_custom_keys();
foreach ( $custom_field_keys as $key => $value ) {
    $valuet = trim($value);
    if ( '_' == $valuet{0} )
        continue;
    echo $key . " => " . $value . "<br />";
}
?>

If the post contains custom fields with the keys mykey and yourkey, the output would be something like:

0 => mykey
1 => yourkey

Note: Regardless of how many values (custom fields) are assigned to one key, that key will only appear once in this array.

get_post_custom_keys() 示例

源文件

get_post_custom_keys() 函数的代码位于 wp-includes/post.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve meta field names for a post.
 *
 * If there are no meta fields, then nothing (null) will be returned.
 *
 * @since 1.2.0
 *
 * @param int $post_id Optional. Post ID. Default is ID of the global $post.
 * @return array|void Array of the keys, if retrieved.
 */
function get_post_custom_keys( $post_id = 0 ) {
	$custom = get_post_custom( $post_id );

	if ( !is_array($custom) )
		return;

	if ( $keys = array_keys($custom) )
		return $keys;
}

get_post_custom_keys() 源文件

相关