简体   繁体   中英

Retrive data from ACF fields and show in frontend table in Wordpress

I want to show a dynamic table in index.PHP which retrieve data from ACF field every time when new entry and display.I am new in php and does not know the exact method. Here is my cod,

<div class="table">
  <?php
  $args = array( 'post_type' => 'Team', 'posts_per_page' => 20 );
  $loop = new WP_Query( $args );
  `while ( $loop->have_posts() ) : $loop->the_post();
  ?>
  endwhile
  ?>
  <table class="tb1" style="width:50%">
    <tr>
      <th > Name</th>
      <th >Email</th>
      <th >Phon no</th>
      <th >Designation</th>
    </tr>
    <tr>
      <td ><?php  echo get_field('name');?></td>
      <td ><?php  echo get_field('email');?></td>
      <td><?php  echo get_field('phon_no');?></td>
      <td><?php  echo get_field('designation');?></td>
    </tr>
  </table>

Please use this code.

<div class="table">
  <?php
  $args = array( 'post_type' => 'Team', 'posts_per_page' => 20 );
  $loop = new WP_Query( $args );  
  ?>
  <table class="tb1" style="width:50%">
    <tr>
      <th >Name</th>
      <th >Email</th>
      <th >Phon no</th>
      <th >Designation</th>
    </tr>
    <?php 
      while ( $loop->have_posts() ) : $loop->the_post();
      ?>
      <tr>
        <td ><?php  echo get_field('name');?></td>
        <td ><?php  echo get_field('email');?></td>
        <td><?php  echo get_field('phon_no');?></td>
        <td><?php  echo get_field('designation');?></td>
      </tr>
      <?php
      endwhile?>    
  </table>

Here would be the recommended approach to using ACF in a normal loop. Note that your while statement is what sets up retrieving each entry, so anything you want to happen for each entry happens inside that loop.

<?php
  $args = array('post_type' => 'Team', 'posts_per_page' => 20);
  $loop = new WP_Query($args);

if ($loop->have_posts()): ?>
  <div class="table">
    <table class="tb1" style="width:50%">
      <tr>
        <th> Name</th>
        <th>Email</th>
        <th>Phon no</th>
        <th>Designation</th>
      </tr>

      <?php while ($loop->have_posts()): $loop->the_post();?>
        <tr>
          <td><?php the_field('name');?></td>
          <td><?php the_field('email');?></td>
          <td><?php the_field('phon_no');?></td>
          <td><?php the_field('designation');?></td>
        </tr>
      <?php endwhile;?>
    </table>
  </div>
<?php endif;?>

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