【问题标题】:How to get a wordpress post custom filed value and assign it to a variable?如何获取 wordpress 发布自定义字段值并将其分配给变量?
【发布时间】:2014-10-10 06:59:32
【问题描述】:

我正在创建一个表单,将表单的详细信息发送到我的电子邮件。

我的自定义字段是:

我的函数.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;
}

我的表格是:

<?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
}  
?>

但是当提交表单时,我得到的只是hotel contact number 的空白值。我做错了什么?提交表单时如何获取正确的自定义字段值?

正如我提到的here,如果我为此使用隐藏字段,无论如何它都可以正常工作: 看看我提到隐藏字段的那个问题的底部区域。

【问题讨论】:

    标签: php wordpress forms email


    【解决方案1】:

    使用以下代码更改功能:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 2020-08-07
      • 2013-06-25
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多