0

I'm trying to show the data inserted into custom post types in a table and I'm having trouble showing values in front end underneath each column i.e (Duration, Incall, Outcall)

enter image description here

enter image description here

                <table class="rates-table">
                <?php $get_rates_list = get_field('rates_optional');
                        if(get_rates_list){
                            foreach($get_rates_list as $rate){?>
                    <thead>
                        <tr>
                        <td><h3 class="inside-model-single">Duration</h3></td>
                        <td><h3 class="inside-model-single">Incall</h3></td>
                        <td><h3 class="inside-model-single">Outcall</h3></td>
                        </tr>
                    </thead>
                    <tbody>
                            
                            <tr>
                            <td></td>
                            <td><span class="rate_value"><?php echo $rate['incall_1_hour'];?></span></td>
                            <td><span class="rate_value"><?php echo $rate['outcall_1_hour'];?></span></td>
                          </tr>
                        <?php }
                        }?>

                        </tbody>
                    </table>

1 Answer 1

1

One way is displaying by its Name individually using get_field() or the_field().

<table class="rates-table">
  <?php $incall_1_hour = get_field('incall_1_hour'); ?>
  <thead>
    <tr>
      <td><h3 class="inside-model-single">Duration</h3></td>
      <td><h3 class="inside-model-single">Incall</h3></td>
      <td><h3 class="inside-model-single">Outcall</h3></td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td><span class="rate_value"><?php echo $incall_1_hour; ?></span></td>
      <td><span class="rate_value"><?php the_field('outcall_1_hour'); ?></span></td>
    </tr>
  </tbody>
</table>

Second way is using get_fields() to get an array of all field values for current or a specific post. Following is just a simple example as I don't know the full structure of your post. Make your changes accordingly.

<?php $get_rates_list = get_fields();

if ($get_rates_list) {
  foreach($get_rates_list as $rate) {
    ?><td><span class="rate_value"><?php echo $rate;?></span></td><?php
  }
} ?>
Sign up to request clarification or add additional context in comments.

4 Comments

This works fine how can I change this to table? <?php $get_rates_list = get_field('rates'); if(get_rates_list){ foreach($get_rates_list as $rate){?> <div class="row"> <div class="col"> <span class="rate_label"><?php echo $rate['rate_title'];?></span> </div> <div class="col"> <span class="rate_value"><?php echo $rate['incall'];?></span> </div> <div class="col"> <span class="rate_value"><?php echo $rate['outcall'];?></span> </div> </div> <?php } }?>
does echo $rate['rate_title'] display the required value?
Well I have created 2 different options in ACF one with the title and the other without title just the repeater so it shows the title which not not set but after someone add's a post item with title and it get's that title and display it. The above code comment code works fine just want to convert it to a formatted table instead of row and col's
ah, ok. If its repeater field then it should work fine. your if (get_rates_list) { is missing $. Here is an example for the table with foreach loop. again make your changes accordingly. gist.github.com/zohaib87/71ca0f36a84ad8f6548a4742af0e8225

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.