Home Forums Front-end Issues Frontend display in table form


Frontend display in table form

  • Hi,

    I would like to display the custom fields in the frontend in table form.

    I want to hide empty fields and I want to have all fields to be automatically shown, even new once.

    This is what I have right now (I have to codes):

    1. Code example


    $fields = get_field_objects();

    if( $fields )
    foreach( $fields as $field_name => $field )
    echo ‘<table>’;

    echo ‘<tr>’;
    echo ‘<td>’ . $field[‘label’] . ‘</td>’;
    echo ‘<td>’ . $field[‘value’] . ‘</td>’;
    echo ‘</tr>’;

    echo ‘</table>’;


    This one shows all new edit custom fields automatically, but the output is a bit strange ( The rows and columns are not in line and there somehow is an empty row. Also, it shows all fields even if there are empty.

    2. Code example

    <table class=”ACFTable”>
    <col width=”40%”>
    <col width=”60%”>
    <td><?php the_field( ‘datum’ ); ?></td></tr>
    <td><?php the_field( ‘land’ ); ?></td></tr>

    The 2. code shows up in the frontend exactly as I want it to (, but I have to edit all new custom fields every time and it shows all fields even if they are empty.

    Is there a way to get the best of both in one code and hide empty fields?

    I’m very much looking forward to hearing from you.

    Best regards,

    $fields = get_field_objects();
    if( $fields ) {
            foreach ($fields as $field_name => $field) {
              if ($field['value]) {
                    <td><?php echo $field['label']; ?></td>
                    <td><?php echo $field['value']; ?></td>
              } // end if value
            } // end foreach field
    } // end if fields
  • Hi,

    Where to add this code to display custom fields in single product page.

    Thanks in advance,

  • Thanks @hube2 for the code sample. As a rabid non-coder, I agree with the comment above: what/where is the best way to deploy this code?

    And (curiously) why do you suppose this “trivial” bit of display logic has not already been wrapped in a nice configuration interface so that we can pick and place ACF fields into a basic tabular display…skipping the manual PHP altogether?


  • I’m not a developer of ACF, just another user.

    I’m not exactly sure what you mean the second part, but ACF is a plugin that requires the modification of templates and the use of PHP on the front of the site. It is an admin tool for developers that aims to reduce the work involved in creating the admin for custom fields and simplifies the getting and displaying of those fields.

    The code I posted would go in your template file where you want to show the field values on the front of the site.

  • Thanks for the reply. I’m doing the best I can with ACF and Elementor at the moment. It looks to me like there is not much support in the WordPress ecosystem for the *display* of tabluar data (or fields in my case). This is very different to the Drupal approach which provides a clean separation of content-structure-style.

  • No, there isn’t any real support in WP for tables, there never has been.

    No, In WP the template files are a mix of PHP code and HTML, not separation. I personally like it this way.

    Unfortunately, I can’t give you any pointers on elementor, I don’t use these types of tools and prefer coding. ACF is really a tool that’s built for people that like to code the templates themselves. There are other custom field plugins that do more and can remove the need to code and also supply templating features for those that do not do coding

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Frontend display in table form’ is closed to new replies.