Support

Account

Home Forums General Issues keep context in ACF and wp_get_template_part

Helping

keep context in ACF and wp_get_template_part

  • I have a repeater field, and inside the nested repeater field a flexible content field

    Question 1: how can i pass the nested field to get_template_part while preserving scope?

    This code below is working fine:

    	while ( have_rows( 'repeater_page_section' ) ) : the_row();
    			$section_title    = get_sub_field( 'section_title' );
    			$background_color = get_sub_field( 'background_color' );
    	
    			while ( have_rows( 'fexible_content' ) ) : the_row();
    				$layout = get_row_layout();
    				switch ( $layout ) {
    					case 'flex_rich_text_editor':
                                          $content =   get_sub_field( 'rich_text_editor' )
                                    get_template_part(
    							'partials/common/blocks/editor',
    '',
    							[
    								'content' => $content,
    							]
    						);
    

    And I can pass my sub field, once fetched to the template context.
    However what if field ‘rich_text_editor’ is another nested repeater or flexible field?
    How can I call these contextual in-loop methods like have_rows, from within the template part? The scope is lost, as we have to pass variables explicitly into the template.

    So get_row_layout(), have_rows() etc no longer work.

    I also cannot call for example, get_field(‘rich_text_editor’, $postID )
    since rich_text_editor is an nested child of repeater_page_section.

    Question 2: when i use

    get_fields( $post->ID );

    and then dump the output, I see well defined nested structure of my data.
    what is the advantage of using these proprietary methods like have_rows, the_row, get_field, get_sub_feild etc
    instead of just iterating through the returned data structure in a foreach ?

  • get_template_part() now allows you to pass variables to templates.

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

You must be logged in to reply to this topic.