Support

Account

Home Forums Add-ons Flexible Content Field Flexible Content to Releationship to Flexible Content

Solving

Flexible Content to Releationship to Flexible Content

  • Hi!

    Im sitting and developing a site where I use the Flexible Content and in one of my “layouts” i use Relationship to a post type called “widget”.

    In this widget I use Flexible Content again and when I’m trying to get a field from this Flexible Content it’s dead end.

    I have tried to query the post type “widget” directly and output the data from Flexible Content and that’s fine. But when I’m having the Flexible Content releationship to pick up the data from this post type’s Flexible Content it’s not working like I want it too.

    This is my code so far:

    <?php if( have_rows('modules') ): ?>
    <?php while ( have_rows('modules') ) : the_row(); ?> 
    
    <?php if( get_row_layout() == 'content' ): ?>
    
    <?php $widgets = get_sub_field('widgets'); if($widgets && !get_sub_field('full_width')): ?>
    <?php foreach( $widgets as $post): ?>
    <?php setup_postdata($post); ?>
    
    <?php if( have_rows('widgets') ): ?>
    <?php while ( have_rows('widgets') ) : the_row(); ?>
    <!-- I can get it to output here, but not further... -->
    
    <?php if( get_row_layout() == 'test' ): ?>
    <!-- Here it SHOULD output something, but it doesn't. Why? -->
    <?php endif; ?>
    
    <?php endwhile; ?> 
    <?php endif; ?>
    
    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>
    
    <?php endif; ?>
    
    <?php endwhile; ?>    
    <?php endif; ?>
  • Hi @henricakesson

    What happens if you add the post ID to the inner flexible field?

    
    <?php if( have_rows('widgets', $post->ID) ): ?>
    <?php while ( have_rows('widgets', $post->ID) ) 
    
  • Hi!

    I didn’t have to try that way now because I solved the problem by looking through my code once again and I found the damn problem…

    When I check for a relationship I used this code:

    <?php $widgets = get_sub_field('widgets'); if($widgets && !get_sub_field('full_width')): ?>
    <?php foreach( $widgets as $post): ?>
    <?php setup_postdata($post); ?>

    And when I then checked for the Flexible Content inside this “widget” I used this code:

    <?php if( have_rows('widgets') ): ?>
    <?php while ( have_rows('widgets') ) : the_row(); ?>

    This obviously didn’t work so I figured out that maybe the name of my flexible content “widgets” was conflicting with the relationship get_sub_field(‘widgets’). So I changed name on my Flexible Content field to “widgets_flexible” and it worked…

    Thanks anyway!

  • Hi @henricakesson

    Ah I see! Glad it worked out then! 🙂

    Please feel free to post any other issues with ACF you have in the future.
    Have a nice weekend.

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

The topic ‘Flexible Content to Releationship to Flexible Content’ is closed to new replies.