Search code examples
resetvar-dump

Why is my $first-colour variable not dumping?


I'm making a button repeater inside a building block builder all created through Advanced Custom Fields and everything is working and printing fine except where I get to the part where I have to reset my $button_colour. It seems to be resetting it okay but when I var dump the new variable ($first_colour) its returning "null".

The aim is for them to be able to select one of three dropdowns (black, green or white) and it adds that as a class to the button giving it all its styling accordingly.

I have it working on another project I did a while ago so I've pulled this code from a project that used "Custom Field Suite" so I understand this may not be the right way of doing it.

Any help would be much appreciated.

echo "<div class=\"block-inner\">\n";
$buttons = get_sub_field('button');
var_dump($buttons);

if(!empty($buttons)){
    echo "<div id=\"button-wrapper\" class=\"button-wrapper\">\n";
    foreach($buttons as $button){
        $button_title = $button['button_title'];
        $button_colour = $button['button_colour'];
        $background_colour = $button['background_colour'];
        $add_class = $button['add_a_class'];
        $link_url = $button['destination_url'];
        var_dump($add_class);

        reset($button_colour);
        $first_colour = key($button_colour);
        var_dump($first_colour);


        echo "<div id=\"button-container\" class=\"button-container\">\n";
        printf("<a class='%s %s' href='%s'>%s</a>\n",

                ($first_colour=="green")?"green-btn":(
                                ($first_colour=="white")?"white-btn":(
                                    ($first_colour=="black")?"black-btn":""
                                )
                            ),
                !empty($add_class)?$add_class:"",
                            $link_url,
                            $button_title
                        );



    }
}

echo "</div>\n";
echo "</section>\n";er code here

Solution

  • I found the answer myself you dont need the reset at all with ACF just go straight to the == check. Code is below:

        echo "<div class=\"block-inner\">\n";
    $buttons = get_sub_field('button');
    //var_dump($buttons);
    
    if(!empty($buttons)){
        echo "<div id=\"button-wrapper\" class=\"button-wrapper\">\n";
        foreach($buttons as $button){
            $button_title = $button['button_title'];
            $button_colour = $button['button_colour'];
            $background_colour = $button['background_colour'];
            $add_class = $button['add_a_class'];
            $link_url = $button['destination_url'];
            //var_dump($add_class);
            //var_dump($button_colour);
    
            echo "<div id=\"button-container\" class=\"button-container\">\n";
            printf("<a class='%s %s' href='%s'>%s</a>\n",
    
                    ($button_colour=="green")?"green-btn":(
                                    ($button_colour=="white")?"white-btn":(
                                        ($button_colour=="black")?"black-btn":""
                                    )
                                ),
                    !empty($add_class)?$add_class:"",
                                $link_url,
                                $button_title
                            );
    
    
    
        }
    }
    
    echo "</div>\n";
    echo "</section>\n";