Home › Forums › Front-end Issues › Div Background color ACF
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?
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 ^^
@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?
The topic ‘Div Background color ACF’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.