Wordpress 提取高级自定义字段';数据一次全部保存在一个单独的PHP文件中

Wordpress 提取高级自定义字段';数据一次全部保存在一个单独的PHP文件中,wordpress,advanced-custom-fields,Wordpress,Advanced Custom Fields,有时,一个页面会使用几十个ACF字段,例如中继器字段。几十个ACF字段意味着几十个如果(get_field('blablabla'){the_field('blablabla');}(以及其他ACF代码)阻塞了页面的PHP模板文件,使其难以有效读取和维护,我想将所有ACF数据提取到PHP变量中,以便模板使用 问题:如果我这样做,我需要在模板开始时将我的所有PHP变量声明为全局变量 我还没有想到任何替代方法?我认为对于repeater字段,您可以在同一页面中为每个特定任务使用一个函数,并在同一文件

有时,一个页面会使用几十个ACF字段,例如中继器字段。几十个ACF字段意味着几十个
如果(get_field('blablabla'){the_field('blablabla');}
(以及其他ACF代码)阻塞了页面的PHP模板文件,使其难以有效读取和维护,我想将所有ACF数据提取到PHP变量中,以便模板使用

问题:如果我这样做,我需要在模板开始时将我的所有PHP变量声明为全局变量


我还没有想到任何替代方法?

我认为对于repeater字段,您可以在同一页面中为每个特定任务使用一个函数,并在同一文件中需要时调用该函数。例如:

<html>
    <head>
    </head>
    <body>
        <div><?php call_user_func( 'who_weve_worked_with', get_the_ID() ); ?></div>
    </body>
</html>
它需要包含在functions.php中

<?php require get_template_directory() . '/inc/acf-fucntions.php'; ?>

关于儿童主题

<?php require get_stylesheet_directory() . '/inc/acf-fucntions.php'; ?>

现在,在acf-functions.php中

<?php
if( !function_exists( 'who_weve_worked_with' ) ) {

function who_weve_worked_with( $post_id )   {

    ?>
    <div>      
        <ul class="nav nav-tabs">
            <?php
            if ( have_rows( 'repeater_field_name', $post_id ) ) :
                $i = 0;
                while ( have_rows ( 'repeater_field_name', $post_id ) ) : the_row();
                    $img = get_sub_field( 'repeater_sub_field_name', $post_id );
                    ?>
                    <li role="presentation">

                        <img src="<?php echo $img[ 'url' ]; ?>" alt="<?php echo $img[ 'alt' ]; ?>">

                    </li>
                    <?php
                    $i++;
                endwhile;
            endif;
            ?>
        </ul>
    </div>
    <?php
    }
}

      
  • “alt=”“>

谢谢。如果我需要在模板中的一个位置使用所有ACF数据,这将非常有效。问题是我需要检索许多ACF字段值,并在模板中的许多不同位置使用这些值。
<?php
if( !function_exists( 'who_weve_worked_with' ) ) {

function who_weve_worked_with( $post_id )   {

    ?>
    <div>      
        <ul class="nav nav-tabs">
            <?php
            if ( have_rows( 'repeater_field_name', $post_id ) ) :
                $i = 0;
                while ( have_rows ( 'repeater_field_name', $post_id ) ) : the_row();
                    $img = get_sub_field( 'repeater_sub_field_name', $post_id );
                    ?>
                    <li role="presentation">

                        <img src="<?php echo $img[ 'url' ]; ?>" alt="<?php echo $img[ 'alt' ]; ?>">

                    </li>
                    <?php
                    $i++;
                endwhile;
            endif;
            ?>
        </ul>
    </div>
    <?php
    }
}