0

i currently have the following:

<?php

$field_name = "text_field";
$field = get_field_object($field_name);   

<table>
    <tbody>
    if( isset($field['value'] ): ?> 
        <tr>
            <th><?php echo $field['label']; ?></th>
            <td><?php echo $field['value']; ?></td>
        </tr>
    <?php endif; ?>
    </tbody>
</table>

my goal is to make the entire table row collapse and not display if there is no value entered.

clearly a novice. thanks for taking a look.

4
  • I was going to answer then I saw you edited it. Doesn't this new version work? It looks fine.
    – VesterDe
    Commented Oct 26, 2016 at 18:35
  • gives an error. perhaps syntax? d the echoes in the if statement need to be wrapped or something? Commented Oct 26, 2016 at 18:46
  • 2
    Oh, silly me, I'm blind... you need <?php before the if statement :P
    – VesterDe
    Commented Oct 26, 2016 at 19:04
  • 1
    ^^ what VesterDe said!
    – jdm2112
    Commented Oct 26, 2016 at 19:06

2 Answers 2

2

You have a mismatch in your PHP tags. Use this:

<?php
$field_name = "text_field";
$field = get_field_object($field_name);   
?>

<table>
    <tbody>
    <?php
    if( isset($field['value'] ): ?> 
        <tr>
            <th><?php echo $field['label']; ?></th>
            <td><?php echo $field['value']; ?></td>
        </tr>
    <?php endif; ?>
    </tbody>
</table>
1

As per ACF documentation, field[‘value’] will always be set.

Instead do if (!empty($field['value']) or just if ($field['value']).

Thus it should look like this:

<?php
$field_name = "text_field";
$field = get_field_object($field_name);   
?>

<table>
    <tbody>
    <?php
    if ($field['value']): ?> 
        <tr>
            <th><?php echo $field['label']; ?></th>
            <td><?php echo $field['value']; ?></td>
        </tr>
    <?php endif; ?>
    </tbody>
</table>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.