Support

Account

Home Forums Add-ons Flexible Content Field Clone partial used in multiple contexts

Solved

Clone partial used in multiple contexts

  • I have a clone field that is used in 2 places:

    – Homepage
    – Flexible Content field

    I have a template partial that displays the fields from the clone.

    I include the partial in the Homepage template and I include the partial in the flexible content field.

    The partial uses get_field to display each field.

    When included in the homepage, the fields are displayed.

    When included in the flexible content field, the fields are not displayed, because it needs to use get_sub_field.

    Is there a way to have 1 clone field partial that can be used for a page, and also within a flexible content field?

    Thanks

    Homepage

    get_template_part( 'components/clones/split-col');

    Flexible content

    if( have_rows('page_builder_content_blocks') ):
    
      while ( have_rows('page_builder_content_blocks') ) : the_row();
    
        if( get_row_layout() == 'page_builder_split_column' ):
    
          get_template_part( 'components/clones/split-col');      
    
        endif;
    
      endwhile;
    
    endif;

    Partial

     <div>
        <?php if (get_field('split_col_title')) : ?>
          <h2><?php the_field('split_col_title'); ?</h2>
        <?php endif; ?>
    
        <?php if (get_field('split_col_text')) : ?>
          <?php the_field('split_col_text'); ?>
        <?php endif; ?>
      </div> 
  • In your home page or anywhere that the clone field in not in a repeater (flex field) you can clone the field into a group field.

    Add a have_rows() loop on the group field and include the same template part

  • Yes that works, thank you

    if( have_rows('home_page_intro') ):
      while ( have_rows('home_page_intro') ) : the_row();
        get_template_part( 'components/clones/split-col');
      endwhile;
    endif;
  • If you clone a field group you should never need to update the field groups that they are cloned into. The clone pulls in the fields in the cloned group, it does not copy the cloned group.

    How I get around the get)field() or get_sub_field() issue is that I don’t. When I clone a group of fields the clone is in a group field. This means that they are always sub fields and that the field names till not conflict. Here’s an example. I have a component field group for CTAs. I use it everywhere to allow clients to add CTA buttons to damn near anything. My flex layouts all have an optional header and footer. In the header section there is a field names “header_ctas” that is a group field and the only sub field is the clone of my cta component group. Every layout also has an optional footer and in this section there is a group field named “footer_cta”. Then in my template file for either the header of the footer, or anywhere else I simply do

    
    if (have_rows('group_field_name')) {
      while (have_rows('group_field_name')) {
        the_row();
        get_template_part('template-parts/components/ctas');
      }
    }
    
  • Good to know, thanks John.

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

The topic ‘Clone partial used in multiple contexts’ is closed to new replies.