Support

Account

Home Forums General Issues ACF functions inside PHP functions

Solved

ACF functions inside PHP functions

  • So I have a plugin I am working on and in that plugin, currently, I am using the same code in multiple files. I would like to stop that and use the code once but in the code, I make calls to functions like get_sub_field() and others. So what I am wondering is how can I take the code that calls those functions and put it inside my own function and those ACF functions still work? For example in the code below I would like to take everything inside the while (have_rows(‘tb_table_operators’)) loop and put it inside my own function and then just call that function inside the loop. So is there any way I can actually do this?

    $item_data = new WP_Query($item_args);
    if ( $item_data->have_posts() ) {
    while ($item_data->have_posts()) {
    $item_data->the_post();
    if(have_rows(‘tb_table_operators’)) {
    while (have_rows(‘tb_table_operators’)) {
    if(have_rows(‘custom_fields’)) {
    while (have_rows(‘custom_fields’)) { the_row();
    if(get_row_layout() == ‘short_text’) {
    if(get_sub_field(‘short_text’)) {
    $custom_fields[get_sub_field(‘short_text_id’)][‘val’] = get_sub_field(‘short_text’);
    }
    }
    }//while (have_rows(‘custom_fields’))
    }//if(have_rows(‘custom_fields’))
    }//while (have_rows(‘tb_table_operators’))
    }//if(have_rows(‘tb_table_operators’))
    }//while ($item_data->have_posts())
    }//if ( $item_data->have_posts() )

  • After looking through some of the functions I am using I notice that ACF sets a Global when running a loop. So the functions should just work if the global has been set.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.