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 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?