0

I'm new to utilizing PHP fully, so I think this is just my lack of understanding the proper syntax.

I've got a home page that's being built with WordPress and Advanced Custom Fields. The images are the same thing over and over again for different data, so proper time to create a function correct?

This is my function:

<?php
    function homeMobImgLayout($flexibleLayout) {
        $pageID = '333';

        // check if the flexible content field has rows of data
        if( have_rows('$flexibleLayout', $pageID ) ):

         // loop through the rows of data
        while ( have_rows('$flexibleLayout', $pageID ) ) : the_row();


        // 100% Image
        if( get_row_layout() == 'image_100' ):

        $img = get_sub_field('img');
        $alt = get_sub_field('alt');

        echo '<div class="imagePod width100 clearfix">';
        echo '<img src="' . $img . '" alt="' . $alt . '" />';
        echo '</div>';


        // 50% Image
        elseif( get_row_layout() == 'image_50' ):

        $img = get_sub_field('img');
        $alt = get_sub_field('alt');

        echo '<div class="imagePod width50 clearfix">';
        echo '<img src="' . $img . '" alt="' . $alt . '" />';
        echo '</div>';

        endif;

        endwhile;

        else :
        // no layouts found
        endif;
        }
?>

The only thing that needs to change with each instance of the function call is the two places where I've got the variable $flexibleLayout

(if( have_rows('$flexibleLayout', $pageID ) ):

while ( have_rows('$flexibleLayout', $pageID ) ) : the_row();

So then with each instance I call the function, I though I just passed what I wanted this value to be.

Examples:

<?php
    homeMobImgLayout("pet_boarding_imgs");
?>

and

<?php
    homeMobImgLayout("dog_boarding_imgs");
?>

Where is my understanding and/or syntax incorrect?

1 Answer 1

2

'$flexibleLayout' is literally the string "$flexibleLayout".
If you want to use a variable, use a variable (without quotes):

if (have_rows($flexibleLayout, $pageID)) :
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much, @deceze. I knew it was something simple I overlooked. Appreciate it. Will accept the answer after the allotted minimum time passes.

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.