簡體   English   中英

如何以及在何處獲取“密鑰”,然后在get_post_meta()中使用它?

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

我正在嘗試使用get_post_meta($ post-id,$ key)方法,但是由於我不知道該帖子的鍵名,因此遇到了一些問題。 我如何獲得鑰匙? 這段代碼只是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>

如果您有腳本或某種方式來獲取帖子的所有鍵,那將非常感謝!

最好的問候,Ledung。

您可以使用get_post_custom_keys來獲取與帖子相關的所有元鍵。 它返回一個數組。 這是來自法典的一個例子:

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

這是指向法典的鏈接: https : //developer.wordpress.org/reference/functions/get_post_custom_keys/

沒有鍵的get_post_meta()函數返回特定帖子ID的所有帖子元的數組:

$post_meta = get_post_meta(get_the_id());
print_r($post_meta); // Shows all 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