Home › Forums › Front-end Issues › Displaying a field that is conditional to another
Hey guys, new to ACF. First of all, sorry if this has been answered before. I haven’t been able to find it.
I have a couple of custom fields on a custom post type which are conditional to another:
I have “brands” wich is a select with a list of brands
and then “brandA-models”, “brandB-models”, “brandB-models” etc which are a select of models for each brand and depend on what was selected as brand. On Back-end they work perfect. What I dont know how to do is display this info on the post.
I don’t need this to be dinamical, it’s more of a tech-sheet which lists brand-model-etc
I have tried this but doesn’t work (my php it’s beginners level at best, i know):
<li>Brand: <strong><?php the_field('brand') ?></strong></li>
<li>Model: <strong> <?php
$brand = get_field('brand');
$modelo_brand = "modelo_" . $brand;
if(get_field('marca'))
{
echo '' . get_field('$modelos_marca') . '';
}
?></strong></li>
Could someone please point me in the right direction?
Thanks!!
Hi @cezilia_mar
Not sure if I’m understanding your query, however the 1 problem I can see in your code is this: get_field('$modelos_marca')
If you’re fetching a variable, you can drop the quotes so that it becomes: get_field($modelos_marca)
So your code might be:
$brand = get_field('brand');
$modelo_brand = "modelo_" . $brand;
if(get_field('marca')) {
echo get_field($modelo_brand);
}
Alternatively, perhaps you could clarify your problem?
Yes I definitely think the code I wrote wont work.
What I need is to display a field that has conditional logic to another.
I have a list of brands and models. To display brand is easy I just use <?php the_field(‘brand’) ?>
Now to display a model that has a conditional logic to that brand, that is when I find myself in trouble.
With your help I managed echo the name of the field.
<?php
$brand = get_field(‘brand’);
$model = “model_”;
$model_brand = $model . $brand;
echo $model_brand;
?>
What I need is a way to use this stored in the new variable “model_brand” to get the field “model_brand” to give you an example the field could be named “model_ford”. I am not sure this is the right path, probably not. But I imagine there has to be a way to display a field with conditional logic, right?
So
1) you need to fetch the name of the brand, ie ford
or bmw
and you currently do this with $brand = get_field('brand');
2) you then need to fetch the models using this brand variable and your naming structure is model_$brand
essentially. to do this you’d then say $models = get_field('model_'.$brand);
Have you tried this?
What do you see if you try do print_r($models)
?
You must be logged in to reply to this topic.
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!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.