【问题标题】:Send Custom Post type fields to multiple emails将自定义帖子类型字段发送到多封电子邮件
【发布时间】:2018-02-08 23:25:18
【问题描述】:

实际上我想做的是我想将自定义字段值发送到电子邮件。

我创建了一个帖子类型的简单表单,该表单仅在前端显示,用户可以在那里输入电子邮件和电话号码。提交后,一封邮件发送给网站所有者,一封邮件发送给企业所有者,另一封邮件发送给该特定用户的电子邮件 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 = 'siteowner@site.com';
        $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 = 'businessowner@business.co.in';
        $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; ?>

【问题讨论】:

    标签: php wordpress forms email


    【解决方案1】:

    使用get_field() 而不是the_field() 来正确分配变量。 the_ 附和它。

    即:

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

    电子邮件的问题是因为您在循环中发送电子邮件。这意味着它正在检查是否为每个循环设置了if(isset($usermail) &amp;&amp; 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){
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-06
      • 2011-02-08
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多