duanchui1955 2014-10-10 06:59
浏览 49
已采纳

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

I am creating a form that sends the form's details to my email.

My custom field is:

enter image description here

My 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. 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.

  • 写回答

1条回答 默认 最新

  • duanmu6231 2014-10-10 07:10
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图