[英]PHP. why is get_post_meta() returning key not value
對 PHP 和 WordPress 相當陌生,並且對 get_post_meta function 有疑問。
所以顯然 function 返回與特定元屬性的鍵關聯的值,但由於某種原因,我得到了返回的鍵。
元屬性是一個帶有幾個鍵值對的選擇框,如下所示:
function frontend_add_donation_field( $fields ) {
$fields['job']['job_donation'] = array(
'label' => __( 'Please specify the required donation amount to be paid by the successful applicant upon completion.', 'job_manager' ),
'type' => 'select',
'options' => array('tier1' => 100, 'tier2' => 200, 'tier3' => 300, 'tier4' => 400, 'tier5' => 500, 'tier6' => 600),
'required' => true,
'placeholder' => 'Please Select',
'priority' => 5
);
return $fields;
}
現在前端的代碼現在是這樣的:
<h4><?php echo get_post_meta( $post->ID, '_job_donation', true ); ?></h4>
我從中得到的是'tier1'等,而不是與之關聯的值。 (我原諒現階段的任何錯誤編碼,例如 escaping html 等。目前只是想獲得一個值)。
執行var_dump(get_post_meta( $post->ID, '_job_donation', true ))
返回string(5) "tier5"
同樣根據評論,插入數組的值是: array('tier1' => 100, 'tier2' => 200, 'tier3' => 300, 'tier4' => 400, 'tier5' => 500, 'tier6' => 600)
更新以下可怕的 HTML 獲得了預期的效果,但確實不合適。
<?php $donation_tier = get_post_meta( $post->ID, '_job_donation', true );
$donation = "";
if ($donation_tier === 'tier1') {
$donation = "100";
}
else if ($donation_tier === 'tier2') {
$donation = "200";
}
else if ($donation_tier === 'tier3') {
$donation = "300";
}
else if ($donation_tier === 'tier4') {
$donation = "400";
}
else if ($donation_tier === 'tier5') {
$donation = "500";
}
else if ($donation_tier === 'tier6') {
$donation = "600";
} else {
$donation = $donation_tier;
}
?>
<?php do_action( 'single_job_listing_meta_end' ); ?>
<li class="meta-important">The successful applicant will need to pay a donation of £<?php echo $donation; ?> plus an £50 standard fee to Horses 4 Homes upon completion.</li>
我想我需要展示的是數組元素的鍵,但不確定是否可以使用 get_post_meta() function 來獲得它。
這是因為我使用了選擇框嗎? 我所有的文本字段似乎都正確呈現。 任何幫助都會很棒。
因為元值是以數組形式存儲的,所以代碼應該是
<?php
$donation_tier = get_post_meta( $post->ID, '_job_donation', false );
$donation = "";
if($donation_tier != null){
foreach($donation_tier as $key => $donation_tier){
if ($donation_tier === 'tier1') {
$donation = "100";
}
else if ($donation_tier === 'tier2') {
$donation = "200";
}
else if ($donation_tier === 'tier3') {
$donation = "300";
}
else if ($donation_tier === 'tier4') {
$donation = "400";
}
else if ($donation_tier === 'tier5') {
$donation = "500";
}
else if ($donation_tier === 'tier6') {
$donation = "600";
} else {
$donation = $donation_tier;
}
}
}
?>
<?php do_action( 'single_job_listing_meta_end' ); ?>
<li class="meta-important">The successful applicant will need to pay a donation of £<?php echo $donation; ?> plus an £50 standard fee to Horses 4 Homes upon completion.</li>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.