简体   繁体   English

如何获取WordPress发布自定义字段的值并将其分配给变量?

[英]How to get a wordpress post custom filed value and assign it to a variable?

I am creating a form that sends the form's details to my email. 我正在创建一个将表单的详细信息发送到我的电子邮件的表单。

My custom field is: 我的自定义字段是:

在此处输入图片说明

My function.php 我的function.php

function getHotel_contact_number() {
    global $wp_query;
    $postid = $wp_query->post->ID;
    $getHotel_contact_number = get_post_meta($postid, 'hotel_contact_number', true);
    $getHotel_contact_number;
}

And my form is: 我的形式是:

<?PHP
$errors = array();
if (isset($_POST["submit"])) {
    $to = "myemail@email.com";
    $subject = "This is my subject";    
    $hotel = get_permalink();
    $hotel_contact_nmbr = getHotel_contact_number();
    $headers = array('From: '.$_POST['sendername'].' <'.$_POST['senderEmail'].'>');
    //Check the name and make sure that it isn't a blank/empty string.
        if(empty($sender)){
            //Blank string, add error to $errors array.        
            $errors['sendername'] = "Please enter your name!";
        }
        if(empty($senderEmail)){
            //Blank string, add error to $errors array.        
            $errors['senderEmail'] = "Please enter your email!";
        }
    $mailBody = "<h3>Hotel Details</h3><br/>
                    $hotel<br/>
                    $hotel_contact_nmbr<br/>"

    $mail_sent = wp_mail( $to, $subject, $mailBody, $headers ); 

}
    if ($mail_sent) { ?>

<h1 style="color: #007f00;">Request sent.</h1>

<?php 
} else {
?>

<form id="" name="" action="<?php echo get_permalink(); ?>" method="post">
    <div class="label-input-wrapper">
        <div class="form-label">Name</div>
        <div class="form-input">
            <input type="text" name="sendername" value="<?PHP if(!empty($errors)) { echo $sender;} ?>" />
            <div class="error-msg">
                <?php if(isset($errors['sendername'])) { echo '<span style="color: red">'.$errors['sendername'].'</span>'; } ?>
            </div>
        </div>
    </div>
    <div class="label-input-wrapper">
        <div class="form-label">E-Mail</div>
        <div class="form-input">
            <input type="email" name="senderEmail" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" required value="<?PHP if(!empty($errors)) { echo $senderEmail;} ?>" />
            <div class="error-msg">
                <?php if(isset($errors['senderEmail'])) { echo '<span style="color: red">'.$errors['senderEmail'].'</span>'; } ?>
            </div>  
        </div>
    </div>

    <input type="submit" value="Submit" name="submit">
</form>

<?php
}  
?>

But when the form is submitted I am getting just a blank value for hotel contact number . 但是提交表格时,我的hotel contact number只是一个空白值。 What I am doing wrong? 我做错了什么? How can get the proper custom filed value when the form is submitted? 提交表单时如何获得正确的自定义字段值?

Anyhow it's working fine if I use hidden fields for this as I mentioned here : Have a look at the bottom area of that question where I mention about hidden fields. 无论如何,如我在这里所述,如果我使用隐藏字段可以很好地工作:看看该问题的底部,我提到了隐藏字段。

Change function with following code: 使用以下代码更改功能:

function getHotel_contact_number() {
   global $wp_query;
   $postid = $wp_query->post->ID;
   $getHotel_contact_number = get_post_meta($postid, 'hotel_contact_number', true);
   return $getHotel_contact_number;
}

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

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