2017-08-31 118 views
1

其實我想要做的是我想發送自定義字段值到電子郵件。發送自定義帖子類型字段到多個郵件

我已經創建了一個簡單的帖子類型的表單,只顯示在這個表單前端用戶可以輸入那裏的電子郵件和電話號碼。在提交一封郵件到網站所有者之後,一封郵件將發送給企業主,一封郵件將發送給該​​特定用戶的電子郵件ID。

所有三個郵件具有不同的內容

現在的問題是

  1. 當我提交了沒有。每個收到的郵件等於沒有。的帖子 例如我有3個帖子項目&當我點擊一個項目並提交時,它發送所有項目的詳細信息。我想限制一個。

  2. 網站所有者和業主獲取正確的信息,但最終用戶無法獲得自定義字段值,自定義字段來自高級自定義字段插件。

有沒有人可以解決這些問題。

感謝

下面是代碼

<div class="column one"> 
    <?php $pack= new WP_Query(array('post_type'=>'packer'));?> 

    <?php while($pack-> have_posts()): $pack->the_post();?> 

<?php 

    $post = get_post($post); 
    $title = isset($post->post_title) ? $post->post_title : ''; 
    $usermail=$_POST['email']; 
    $cellno=$_POST['cellno']; 
    $phone=the_field('phone'); 
    $pdesc=the_field('description'); 

    if(isset($usermail) && isset($cellno)){ 

     $to = '[email protected]'; 
     $subject = 'New Enqiry for'. $title; 
     $body ="The enquiry for $title <br>Customer email is: $usermail<br> Customer Phone is: $cellno"; 
     $headers = array('Content-Type: text/html; charset=UTF-8'); 
     wp_mail($to, $subject, $body, $headers); 


     $to = '[email protected]'; 
     $subject = 'New Customer Enquiry'; 
     $body ="The enquiry by $usermail <br>Cellno is: $cellno"; 
     $headers = array('Content-Type: text/html; charset=UTF-8'); 
     wp_mail($to, $subject, $body, $headers); 


     $to = $usermail; 
     $subject = 'Details For $title'; 
     $body ="Phone $phone <br>Desc is: $pdesc"; 
     $headers = array('Content-Type: text/html; charset=UTF-8'); 
     wp_mail($to, $subject, $body, $headers); 

    } 
?> 

    <div class="column one-fourth"> 
     <h1><?= the_title();?></h1> 

     <?php $post_id= get_post()->ID;?> 

     <img src="<?php the_field('logo');?>" /> 



     <a href="#desc-<?= $post_id;?>" class="fancybox"> 
      <input type="button" name="Details" value="DON'T PRESS THIS BUTTON"/> 
     </a> 
     <div style="display:none" class="fancybox-hidden"> 
      <div id="desc-<?= $post_id;?>"> 
        <?php the_field('description');?> 
       <?php the_field('phone');?> 

       <form action="" method="POST"> 
       <input type="text" placeholder="Phone" name="cellno" id="cellno"> 
       <input type="email" placeholder="Email" name="email" id="email"> 
       <input type="submit" value="Send" name="send" id="send"> 
       </form> 
      </div> 
     </div>      

    </div> 


<?php endwhile; ?> 

回答

0

使用get_field()而不是the_field()正確地分配變量。 the_呼應它。

即:

$phone = get_field('phone'); 
$pdesc = get_field('description'); 

問題與電子郵件,是因爲你的電子郵件在一個循環中發送。這意味着它的檢查是爲每個循環設置if(isset($usermail) && isset($cellno)){,也就是帖子的數量。

通過傳遞帖子ID並確認,解決此問題。 當前POST_ID添加到您的窗體:

<input type="hidden" name="postID" value="<?php echo $post_id; ?>"> 

並檢查它是否等於在循環中目前職位ID。

if(isset($usermail) && isset($cellno) && isset($_POST['postID']) && $_POST['postID'] == $post->ID){ 
+0

非常感謝@ jacob-raccuia它的工作! :) –

相關問題