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

Need complicated if then scenario help WordPress

  • SOLVED

I have a products page that has two requirements:

1) Allows users to add their own image, or it uses a default image.

2) Checks an Advanced Custom Fields checkbox field to determine what size of image to use.

In brief: I have a products site where all seven products fit into two image sizes (for displaying the product).

The products are a single custom post type, and I have an ACF checkbox field for each that identifies what type of product it is. So I need to display a chunk of code (because the CSS changes) based on the check box array.

On top of that, I want to give people the option to upload their own image, which would then be used instead of the one automatically provided. All of this information is stored in Advanced Custom Fields.

Here's the code as close as I know how, but this of course doesn't do it. It will let you see what I'm trying to do, and I've commented over each code section.



<!-- This would be what would show if it's an oil or blend in the ACF check box -->

<?php if (get_field('oil_image')) { ?>

<div class="oil-image"><img src="<?php the_field('oil_image'); ?>" alt="<?php the_title(); ?>"></div>

<?php } else { ?>

<div class="oil-image"><img src="<?php the_field('main_image_link'); ?>" alt="<?php the_title(); ?>"></div>

<?php } ?>



<!-- and here's what would show if anything else. the if statements are allowing them to upload their own images and not have that overwritten with updates. -->

<?php if (get_field('oil_image')) { ?>

<div class="product-image"><img src="<?php the_field('oil_image'); ?>" alt="<?php the_title(); ?>"></div>

<?php } else { ?>

<div class="product-image"><img src="<?php the_field('main_image_link'); ?>" alt="<?php the_title(); ?>"></div>

<?php } ?>

Answers (2)

2015-08-24

Andrea P answers:

Hi there!

I'm not sure I understood what are you trying to do, but here might be some helpful suggestions:


<?php

// get the checkbox
$kind = get_field('checkbox_kind');

// if it is an oil
if ( is_array($kind) && in_array('oil', $kind) ){
// if there is a custom image, display it
if (get_field('oil_image')) { ?>

<div class="oil-image"><img src="<?php the_field('oil_image'); ?>" alt="<?php the_title(); ?>"></div>

<?php // otherwise display the default
} else { ?>

<div class="oil-image"><img src="<?php the_field('main_image_link'); ?>" alt="<?php the_title(); ?>"></div>

<?php
}

} // if it's a blend
elseif ( is_array($kind) && in_array('blend', $kind) ){
// if there is a custom image, display it
if (get_field('blend_image')) { ?>

<div class="oil-image"><img src="<?php the_field('blend_image'); ?>" alt="<?php the_title(); ?>"></div>

<?php // otherwise display the default
} else { ?>

<div class="oil-image"><img src="<?php the_field('main_image_link'); ?>" alt="<?php the_title(); ?>"></div>

<?php
}

}
?>





also, if you want to choose the size of the displayed image, you can select in the acf field to return the image object instead of the url, and then do something like this:


<?php
$size = 'medium';
$thumb = get_field('oil_image');
if ($thumb!=""){
$thumb_url = $thumb['sizes'][$size];
}
?>
<img src="<?php echo $thumb_url; ?>" alt="" />


Kyler Boudreau comments:

Andrea, your code worked brilliantly. Thank you!

One small question - what if I wanted to specify two types in the checkbox array? How would I change this line:

if ( is_array($kind) && in_array('oil', $kind) ){

For example, let's say I wanted oil and blend in the same section?

Thanks!


Andrea P comments:

Hi There!

I'm glad it was what you were looking for. :)

this bit of code here

in_array('oil', $kind)


is checking if 'oil' is present inside the array $kind (which is the content of the checkbox field).

so you can check for more items in this way


if ( is_array($kind) && ( in_array('oil', $kind) || in_array('blend', $kind) ) ){


the above will return true if $kind is an array (which we need because otherwise the code could throw a fatal error), AND if is true one of the other 2 expressions.
so:
return true if $kind is an array AND if 'oil' OR 'blend' are inside that array.

&& means AND
|| means OR
more recent php standards tells to use these symbols inside conditionals, rather than the textual operator

2015-08-24

Arnav Joy answers:

Hi ,
So you need help with giving user to upload image ?