0

I'm trying to return css build with multiple variables in my Wordpress website, its kind of working but it's only returning the first array. If i echo instead of return inside the foreach it shows the code correctly but outside the correct location.

Sorry for the bad code and possible definitions about php. I have only just started to learn php.

The code

 function child_custom_color_css4() {
    function child_custom_color_css2() {

        

        function test_loop_1_beta() {
            $boo = 'soortverhaal0 ';
            $foo = 'soortverhaal1 ';
            return array($boo, $foo);
        }

        $loops = test_loop_1_beta();

        
        
        global $nectar_options;

        $Styled=array();
            foreach ($loops as $loop) {
                
                return '
            
                #test-loop.'. $loop .' {
                background-color: '.esc_attr($nectar_options["accent-color"]).' !important;
                }
                
                ';

        }
        return implode($Styled);
        

    }

    $get_arrays = child_custom_color_css2();
    
    wp_add_inline_style( 'main-styles', $get_arrays);
}
add_action('wp_enqueue_scripts','child_custom_color_css4', 20); '''

Output:

<style id="main-styles-inline-css" type="text/css">

        
            #test-loop.soortverhaal0  {
            background-color: #f04e23 !important;
            }
            
</style>

Edit: what need:

<style id="main-styles-inline-css" type="text/css">

            #test-loop.soortverhaal0  {
            background-color: #f04e23 !important;
            }

            #test-loop.soortverhaal1  {
            background-color: #f04e23 !important;
            }
            
</style>

1 Answer 1

1

I think you could do something like that:

function child_custom_color_css4() {
    function child_custom_color_css2() {

        $loops = [
          'soortverhaal0 ',
          'soortverhaal0 ',
        ];

        global $nectar_options;

        $styled = [];
        
        foreach ($loops as $loop) {
          $styled[] = '#test-loop.' . $loop . ' { background-color: ' . esc_attr( $nectar_options['accent-color'] ) . ' !important; }';
        };

        return implode("\n",$styled);
        
    }

    $get_arrays = child_custom_color_css2();
    
    wp_add_inline_style( 'main-styles', $get_arrays);
}
add_action('wp_enqueue_scripts','child_custom_color_css4', 20);

I do an array push to acumulate each loop into $style array.

I think you would need to define other separator for the implode function like:

return implode("\n",$styled)
Sign up to request clarification or add additional context in comments.

6 Comments

Looks promising thank you, but the end }of the functions are not matching. I thought the } above the return implode("\n",$styled); needs to go, but removing this gives an empty result. The '<style> is complete empty. Not getting error's
yes, sorry that } should be included after the !mportant, closing the style scope. I edited the code, please check now.
Unfortunately I now get no code at all, not even error
Ey man, sorry… i didn’t get a chance to test this code…but I’m seeing other issue in my code… the array is called $styled and we are pushing the data into an array with other name ($styleArray)…soo I edited the code again replacing $styledArray by $styled. Please check again, hope it works
Thanks this is it! (one little thing you changed the loops = with 2 of the same arrays.) I renamed some of the functions to make it more readable in english etc. Is there a way in this platform i can send you the code? Thanks for helping me out and understanding php a lot more!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.