Support

Account

Home Forums Front-end Issues Displaying a field that is conditional to another

Solving

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) ?

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Displaying a field that is conditional to another’ is closed to new replies.