0

I have this WordPress Custom Field PHP Loop:

<?php
    $fields = CFS()->get('image-field');
    foreach ($fields as $field) {
?>

    <a href="<?php echo $field['image-upload']; ?>">Link to image</a>
    <a href="<?php echo $field['image-url']; ?>">Link to image</a>
    <?php echo $field['image-description']; ?>

<?php } ?>

And want to add an If/Else statement, like (this doesn't work):

<?php
    $fields = CFS()->get('image-field');
    foreach ($fields as $field) {
?>

    if ( $field['image-upload'] ) {
        <a href="<?php echo $field['image-upload']; ?>">Link to image</a>
    }
    else {
        <a href="<?php echo $field['image-url']; ?>">Link to image</a>
    }
    <?php echo $field['image-description']; ?>

<?php } ?>

I've found that this works

<?php
    $fields = CFS()->get('image-field');
    foreach ($fields as $field) {
        if ( $field['image-upload'] ) {
        ?>
            <a href="<?php echo $field['image-upload']; ?>">Go To Store!</a>
        <?php
        } else {
            echo $field['image-url'];
        }
        echo $field['image-description'];
        }
?>

But I run into syntax errors with the above code when trying to add HTML.

Edit: Working solution, thanks crystal

    <?php
    $fields = CFS()->get('image-field');
    ?>

    <? foreach ($fields as $field) : ?>
    <ul>
        <? if ( $field['image-upload'] ) : ?>
            <li><a href="<?= $field['image-upload'] ?>">Link to image</a>
        <? else : ?>
            <li><a href="<?= $field['image-url'] ?>">Link to image</a>
        <? endif ?>
            <li><?= $field['image-description'] ?>
    </ul>
    <? endforeach ?>

1 Answer 1

1

HTML should be written in echo or outside of <?php ... ?> like so:

<?php
$fields = CFS()->get('image-field');
foreach ($fields as $field) {

    if ( $field['image-upload'] ) { ?>
        <a href="<?php echo $field['image-upload']; ?>">Link to image</a>
    <?php } else { ?>
        <a href="<?php echo $field['image-url']; ?>">Link to image</a>
    <?php
    }
    echo $field['image-description'];
}
?>

If using short_open_tag:

<?php
$fields = CFS()->get('image-field');
?>

<? foreach ($fields as $field) : ?>
    <? if ( $field['image-upload'] ) : ?>
        <a href="<?= $field['image-upload'] ?>">Link to image</a>
    <? else : ?>
        <a href="<?= $field['image-url'] ?>">Link to image</a>
    <? endif ?>
    <?= $field['image-description'] ?>
<? endforeach ?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! The short_open_tag version worked well for my use case. I forgot to mention that I intended to have the returned values placed in an unordered list. That is where I had ran into syntax issues.

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.