返回介绍

get_post_custom_keys()

发布于 2017-09-10 23:50:30 字数 2659 浏览 1110 评论 0 收藏 0

get_post_custom_keys( int $post_id )

Retrieve meta field names for a post.


description

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

更新日志

Versiondescription
1.2.0Introduced.

相关函数

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文