如何使用变量和参数在 php 函数中使用 have_rows()

How to use the have_rows() within a php function using variables and parameters

我想创建一个 php 函数,它从我的 Wordpress 站点上的 AdvanceCustomFields 插件访问信息。在我的函数中,我使用 have_rows()get_row_layout()the_sub_field() 函数。这些函数是这样使用的:

 <?php 

function accessingMainLayoutField()
{
    if( have_rows('flexible_content_field_name') ):
        while (have_rows('flexible_content_field_name') ) : the_row();
            if(get_row_layout() == 'layout_name'):

                the_sub_field('sub_field_name');

            elseif(get_row_layout() == ''):

                echo "Error2";

            endif;
        endwhile;                               
    else:
        echo "Error1";
    endif;
}
?>

上面的代码是工作代码的示例,但我更愿意使用函数的参数。

这就是一个例子:

<?php 

    function accessingMainLayoutField($mainfield, $layout, $valuefield)
    {
        if( have_rows($mainfield) ):
            while (have_rows($mainfield) ) : the_row();
                if(get_row_layout() == $layout):

                    the_sub_field($valuefield);

                elseif(get_row_layout() == ''):

                    echo "Error2";

                endif;
            endwhile;                               
        else:
            echo "Error1";
        endif;
    }
    ?>

然后调用这个函数我会写:

<?php
function accessingMainLayoutField("'flexible_content_field_name'", "'layout_name'", "'sub_field_name'");
?>

但是由于某种原因,这段代码不起作用。函数 "echoes" "Error1".

直接调用函数,只用一对引号:

accessingMainLayoutField('flexible_content_field_name', 'layout_name', 'sub_field_name');