简体   繁体   中英

PHP Mail Format with ACF Repeater field

I'm developing a module to send mails from a WordPress post, using some custom fields from ACF.

I have an initial structure like:

        $to = $wpp_send_status_email;
        $subject = $wpp_send_status_subject;
        $body = "
        <html>
        <table border='0' cellpadding='0' cellspacing='0' style='background: #fff; max-width:670px;margin:40px auto;text-align:left;color:#000;border: 1.3px solid rgba(0, 0, 0, 0.05);-webkit-border-radius: 10px;-moz-border-radius: 10px;border-radius: 10px;-webkit-box-shadow: 0px 5px 15px -10px rgba(0, 0, 0, 0.2); box-shadow: 0px 5px 15px -10px rgba(0, 0, 0, 0.2)'>

The code works okay, but when I have to use the repeater field with if and else options, I don't have success. My original code is:

<?php if(get_field('to-do')): ?>
  <tbody>
      <tr>
        <th style="font-family:Arial,sans-serif;text-align: left; vertical-align: middle; padding: 0 0 20px 0; font-size: 14px">Tarefa</th>
        <th style="font-family:Arial,sans-serif;text-align: left; vertical-align: middle; padding: 0 0 20px 0; font-size: 14px">Deadline</th>
        <th style="font-family:Arial,sans-serif;text-align: left; vertical-align: middle; padding: 0 0 20px 0; font-size: 14px">Status</th>
      </tr>
    <?php while(has_sub_field('to-do')): ?>
      <tr style="border-top: 1px solid rgba(0, 0, 0, 0.05)">
      <td class="timeline-phase-activity ng-scope" style="text-align: left;vertical-align:middle;font-size:14px;width:60%;font-family:Arial,sans-serif;padding:10px 30px 10px 0"><span><?php the_sub_field('task-name'); ?></span></td>
      <td style="vertical-align:middle;text-align:left;padding-left: 24px;font-size:14px;font-family:Arial,sans-serif;padding:10px 0">
      <span style="padding-right:1.125rem"><?php the_sub_field('deadline'); ?></span>
      </td>
      <td style="text-align:left;vertical-align:middle;font-size:14px;font-family:Arial,sans-serif;;padding:10px 0"><span><?php the_sub_field('status'); ?></span>
      </td>
      </tr>
    <?php endwhile; ?>
  </tbody>
<?php endif; ?>

Does anyone know how I include this inside HTML tag? My other field is on the " . get_field('conclusion', $post->ID) . " structure, but this doesn't work with if and else.

If I got your question correctly, you're looking for a way to get this content into the $body variable. This can be best achieved using output buffering like so:

  $to = $wpp_send_status_email;
  $subject = $wpp_send_status_subject;
  ob_start(); // start the output buffering
  if(get_field('to-do')): ?>
  <tbody>
      <tr>
        <th style="font-family:Arial,sans-serif;text-align: left; vertical-align: middle; padding: 0 0 20px 0; font-size: 14px">Tarefa</th>
        <th style="font-family:Arial,sans-serif;text-align: left; vertical-align: middle; padding: 0 0 20px 0; font-size: 14px">Deadline</th>
        <th style="font-family:Arial,sans-serif;text-align: left; vertical-align: middle; padding: 0 0 20px 0; font-size: 14px">Status</th>
      </tr>
    <?php while(has_sub_field('to-do')): ?>
      <tr style="border-top: 1px solid rgba(0, 0, 0, 0.05)">
      <td class="timeline-phase-activity ng-scope" style="text-align: left;vertical-align:middle;font-size:14px;width:60%;font-family:Arial,sans-serif;padding:10px 30px 10px 0"><span><?php the_sub_field('task-name'); ?></span></td>
      <td style="vertical-align:middle;text-align:left;padding-left: 24px;font-size:14px;font-family:Arial,sans-serif;padding:10px 0">
      <span style="padding-right:1.125rem"><?php the_sub_field('deadline'); ?></span>
      </td>
      <td style="text-align:left;vertical-align:middle;font-size:14px;font-family:Arial,sans-serif;;padding:10px 0"><span><?php the_sub_field('status'); ?></span>
      </td>
      </tr>
    <?php endwhile; ?>
  </tbody>
<?php
  endif;
  $body = ob_get_clean(); // retrieve the output buffer and save to $body

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM