简体   繁体   English

如何以及在何处获取“密钥”,然后在get_post_meta()中使用它?

[英]How and where to get 'key' and then use it in get_post_meta()?

I am trying to use the get_post_meta($post-id, $key) method, but i run into some problem becuase I do not know the key name for the post. 我正在尝试使用get_post_meta($ post-id,$ key)方法,但是由于我不知道该帖子的键名,因此遇到了一些问题。 How can I get the key? 我如何获得钥匙? This code is simply an example for the get_post_meta($post-id, $key) function... 这段代码只是get_post_meta($ post-id,$ key)函数的示例...

<?php
$current_post_meta = get_post_meta(get_the_id(), '$the_key_i_do_not_know');
?>
<html>
<div class="container">
<?php
echo $current_post_meta[0]; //This echoes the post-id of posts with the same key as $the_key_i_do_not_know.
?>
</div>
</html>

If you have a script or some sort of way of getting a post's all keys that would be great thanks! 如果您有脚本或某种方式来获取帖子的所有键,那将非常感谢!

Best regards, Ledung. 最好的问候,Ledung。

You can use get_post_custom_keys in order to get all of the meta keys related to the post. 您可以使用get_post_custom_keys来获取与帖子相关的所有元键。 It returns an array. 它返回一个数组。 Here's an example from codex: 这是来自法典的一个例子:

<?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 />";
}
?>

And here's the link to the codex: https://developer.wordpress.org/reference/functions/get_post_custom_keys/ 这是指向法典的链接: https : //developer.wordpress.org/reference/functions/get_post_custom_keys/

The get_post_meta() function without a key, returns an array of all post meta for a specific post id: 没有键的get_post_meta()函数返回特定帖子ID的所有帖子元的数组:

$post_meta = get_post_meta(get_the_id());
print_r($post_meta); // Shows all post meta

See also: https://developer.wordpress.org/reference/functions/get_post_meta/ 另请参阅: https : //developer.wordpress.org/reference/functions/get_post_meta/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM