0

I wanted to do the DRY approach in my code but I'm having a hard time figuring it out. And also, I want to hide the entire code if there's no image_1. Hope you could help me do the trick.

Here's the code

<div class="col-md-4">
    <?php
    $image = get_field('image_1');
    if(get_field('image_1'))
    {
        echo '<a href="' . get_field('image_link_1') . '">';?>
        <img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
        <?php echo '</a>';
    } else {
        echo '<img src="http://localhost/image.png">';
    } ?>
</div>
<div class="col-md-4">
    <?php
    $image = get_field('image_2');
    if(get_field('image_2'))
    {
        echo '<a href="' . get_field('image_link_2') . '">';?>
        <img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
        <?php echo '</a>';
    } else {
        echo '<img src="http://localhost/image.png">';
    } ?>
</div>
<div class="col-md-4">
    <?php
    $image = get_field('image_3');
    if(get_field('image_3'))
    {
        echo '<a href="' . get_field('image_link_3') . '">';?>
        <img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
        <?php echo '</a>';
    } else {
        echo '<img src="http://localhost/image.png">';
    } ?>
</div>
10
  • 4
    And what exactly is your problem? Commented May 17, 2016 at 7:44
  • @JannisMattheis I want to loop the code. Commented May 17, 2016 at 7:45
  • As Jannis is saying, it's great that you already spent some time trying to figure it out! If you show us how far you've come, we can answer your question what you should do differently! Commented May 17, 2016 at 7:46
  • @stuXnet I can't make the loop work. Commented May 17, 2016 at 7:47
  • You did not implement a loop, yet... :-( Commented May 17, 2016 at 7:48

4 Answers 4

2

You should put differences to arrays and then wrap everything into for loop:

<?php
  $images = array('image_1', 'image_2', 'image_3');
  $links = array('image_link_1', 'image_link_2', 'image_link_3');

  for($i=0; $i<3; $i++){
?>
<div class="col-md-4">
  <?php
    $image = get_field($images[$i]);
    if(get_field($images[$i])){
      echo '<a href="' . get_field($links[$i]) . '">';
  ?>
  <img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
  <?php echo '</a>';
    } else {
      echo '<img src="http://localhost/image.png">';
    }
  ?>
</div>
<?php
  }
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Btw, how can I hide it if no image_1 is uploaded?
@RubyAnnRosales i can think of multiple solutions but the easiest one is probably to change for condition to: for($i=0; $images[0] && $i<3; $i++)
it's not working. the images echoed in else are still showing.
0

Just a hint...:

<?php
  for ($i = 0; $i < 3; $i++) {
    echo "<div class='col-md-4'>" . "\n";
    $image = get_field("image_" . ($i + 1));
    ...
    echo "</div>" . "\n";
  }
?>

Comments

0

Something along these lines should get you started if I'm understanding you correctly:

<?php for ($q = 1; $q <= 3; $q++) {
    $image_loop = 'image_' . $q;

    echo '<div class="col-md-4">';

    if ($image = get_field($image_loop)) {
        echo '<a href="' . get_field('image_link_' . $q) . '">';
?>
        <img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
        <?php echo '</a>';
    } else {
        echo '<img src="http://localhost/image.png">';
    } ?>
</div>

<?php } // end loop ?>

2 Comments

But, how can I hide it if no image_1 is uploaded?
That's left as an exercise for you. Stackoverflow isn't a place to get your code written for you! Hint: maybe test for image_1 before the for loop.
0

The other suggestions here will work as well but here's what I would do:

First arrange the images in an associative array with keys being the image name and values being the image link and then iterate via a foreach loop.

I generally try to not echo HTML unless strictly necessary.

<?php
$array = [
    "image_1" => "image_link_1",
    "image_2" => "image_link_2",
    "image_3" => "image_link_3",
    "image_4" => "image_link_4"
];

foreach ($array as $name => $link):
    $image = get_field($name); 
    if ($image): ?>
    <div class="col-md-4">
        <a href="<?=get_field($link)?>">
            <img src="<?= $image['url']; ?>" title="<?= $image['title']; ?>" alt="<?= $image['alt']; ?>" />        
        </a> 
        <?php else: ?>
            <img src="http://localhost/image.png">
        <?php endif; ?>
    </div>
<?php endforeach; ?>

Comments

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.