Hello, I need to change a div background color based on the color I chose in a custom post field.
Example:
Field name: color
Type of field: text
Choices:
red: Red
blue: Blue
yellow: Yellow
My code
<div class=”title-item”>
“><h4><?php the_title(); ?></h4>
<?php the_field(‘color’, ‘red’);?>
Is that correct?
Thank you
Hi,
What you would need to do would be something along the lines of:
<?php if(get_field('color')) {
$color = get_field('color'); ?>
<div class="title-item" style="background-color:<?php echo $color ?>;">
<?php } else { ?>
<div class="title-item">
<?php } ?>
<h4><?php the_title(); ?></h4>
</div>
Hello, thank you for your reply. I’ve tried your solution but that still doesn’t work (still a better code than mine tho ^^). I’m afraid I’m still missing something.
Hi,
That’s okay, what do you get back if you just use:
<?php if(get_field('color')) {
$color = get_field('color');
echo $color;
}?>
The background color is still blank. What I’m trying to do is for it to change based on the color I chose in my custom post field.
Just to check, what have you called the field and is it inside the loop?
The field is called “color” and yes, it’s inside the loop
Can you take a screenshot of the field set up for me please?
I solved it creating a new field using “picking colours” to get hex color codes ^^
Pleased to hear that you managed to sort it 🙂
@f-dercole howdy
how did you solve it ? Can you actually select color from divi module and attach it to acf ?
If so, What type of acf is it?