get_post_custom_keys() - WordPress 函数参考中文文档

返回介绍

get_post_custom_keys()

发布于 2017-09-10 字数 2661 浏览 784 评论 0

get_post_custom_keys( int $post_id )

Retrieve meta field names for a post.


描述

If there are no meta fields, then nothing (null) will be returned.


参数

$post_id

(int)
(Optional)
Post ID. Default is ID of the global $post.


返回值

(array|void) Array of the keys, if retrieved.


源代码

File: wp-includes/post.php

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;
}

更新日志

Version 描述
1.2.0 Introduced.

相关函数

Uses

  • wp-includes/post.php:
    get_post_custom()

Used By

  • wp-includes/post-template.php:
    the_meta()
  • wp-includes/class-wp-embed.php:
    WP_Embed::delete_oembed_caches()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    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.

    
    <?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.

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!