Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Need to incorporate if/then statement in ACF repeater WordPress

  • SOLVED

I have an ACF repeater field that I show in a template file like this:


<?php while( have_rows('files') ): the_row();

// vars
$title = get_sub_field('title');
$file = get_sub_field('file');
$type = get_sub_field('file_type');

?>

<div class="business-doc">
<a href="<?php echo $file; ?>"><img src="<?php home_url(); ?>/media/business-doc.png" alt="<?php the_title(); ?>"></a>
<div class="business-doc-title"><?php echo $title; ?></div>
</div>

<?php endwhile; ?>


What I need to do is incorporate a way to do if else statements. The variable '$type' is a select field that allows for either doc, pdf or pptx to be selected. I want the image above to change based on the selection. So if someone selects PDF, I want to show a PDF icon, etc.

Hopefully I'm explaining this right.

Answers (4)

2015-07-29

Hariprasad Vijayan answers:

Hi,

Try this

<?php while( have_rows('files') ): the_row();
// vars
$title = get_sub_field('title');
$file = get_sub_field('file');
$type = get_sub_field('file_type');
$type_url = '';
if($type == 'doc' || $type == 'DOC')
{
$type_url = get_home_url().'/media/business-doc.png';
}
else if($type == 'pdf' || $type == 'PDF')
{
$type_url = get_home_url().'/media/business-pdf.png';
}
else if($type == 'pptx' || $type == 'PPTX')
{
$type_url = get_home_url().'/media/business-pptx.png';
}
?>
<div class="business-doc"> <a href="<?php echo $file; ?>"><img src="<?php echo $type_url; ?>" alt="<?php the_title(); ?>"></a>
<div class="business-doc-title"><?php echo $title; ?></div>
</div>
<?php endwhile; ?>



Kyler Boudreau comments:

Hariprasad - I tried your code first since you posted first. It worked perfectly. Thank you!

2015-07-29

rochester answers:

Guys, switch is the way to go.

If you add 10 more types this would be unmanageable (DRY - Don't Repeat Yourself): http://php.net/manual/en/control-structures.switch.php

<?php
while( have_rows('files') ): the_row();
$title = get_sub_field('title');
$file = get_sub_field('file');
$type = get_sub_field('file_type');
$img_url = get_home_url().'/media/';

switch($type) {
case 'pdf':
$img_url .= 'business-pdf.png';
break;
case 'doc':
$img_url .= 'business-doc.png';
break;
case 'pptx':
$img_url .= 'business-pptx.png';
break;
}
?>

<div class="business-doc"> <a href="<?php echo $file; ?>"><img src="<?php echo $img_url; ?>" alt="<?php the_title(); ?>"></a>

<div class="business-doc-title"><?php echo $title; ?></div>

</div>

<?php endwhile; ?>


Kyler Boudreau comments:

Rochester,

I decided to try your condensed code - it works as well. Nice! Thanks. I'll split the vote.

2015-07-29

Darlene Grace Arcenal answers:

<?php while( have_rows('files') ): the_row();



// vars

$title = get_sub_field('title');

$file = get_sub_field('file');

$type = get_sub_field('file_type');



?>



<div class="business-doc">
<?php if ($type == 'doc'): ?>
<a href="<?php echo $file; ?>"><img src="<?php home_url(); ?>/media/business-doc.png" alt="<?php the_title(); ?>"></a>
<?php elseif ($type == 'pdf'): ?>
<a href="<?php echo $file; ?>"><img src="<?php home_url(); ?>/media/business-pdf.png" alt="<?php the_title(); ?>"></a>
<?php else: ?>
<a href="<?php echo $file; ?>"><img src="<?php home_url(); ?>/media/business-pptx.png" alt="<?php the_title(); ?>"></a>
<?php endif;?>
<div class="business-doc-title"><?php echo $title; ?></div>

</div>



<?php endwhile; ?>

2015-07-29

Bob answers:

There is another approach to. Add css class according to type and write css to display image.

this way if you wish to add more type in future there is no need to add extra if else and php code.