首页 > 解决方案 > 从 ACF 字段中检索数据并显示在 Wordpress 的前端表中

问题描述

我想在 index.PHP 中显示一个动态表,每次新条目和显示时都会从 ACF 字段中检索数据。我是 php 新手,不知道确切的方法。这是我的鳕鱼,

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

标签: phpwordpressadvanced-custom-fields

解决方案


请使用此代码。

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

推荐阅读