Support

Account

Home Forums Add-ons Flexible Content Field Get field from post_object in Flexible Content

Solved

Get field from post_object in Flexible Content

  • Hi,

    I’ve got the Flexible Content add-on working for basic fields and that works fine.

    Now I’ve got some CPT that I’d like to insert with the Post Object field. I had it working outside the Flexible Content but I can’t get it to work inside it.

    When I link, for example, two posts, I get two div’s with a class’charts’. But they’re empty. No content, no fields from these posts. There’s something wrong with the way I call the fields I guess. I’ve got no clue how to figure it out.

    My page: http://www.mediadigest.be/reclameinvesteringen/test-met-nieuwe-content-editor/

    
    <?php while(has_sub_field("omd_content")) : // FLEXIBLE CONTENT ?>
    
    <?php if ( get_row_layout() == "text" ) : ?>
    <div class="main"><?php the_sub_field("text"); ?></div>
    
    <?php elseif ( get_row_layout() == "table" ) : ?>
    <div class="omdtable">
    <h3><?php the_sub_field('table_title') ?></h3>
    <div class="table-responsive">
    <?php the_sub_field('table') ?>
    </div>	
    </div> 
    <?php // everything works fine up to here ?>
    
    <?php elseif ( get_row_layout() == "chart_picker" ) : ?>	
    
    										<div class="charts">
    											<?php if ( get_sub_field("chart") ) : ?>
    											<?php foreach ( get_sub_field("chart") as $p ) : ?>
    <?php echo get_permalink($p->ID); ?>
    												<?php if( get_field( 'highcharts_beschrijving_van_de_grafiek', $p->ID) ): ?>
    												<div class="description"><?php the_field('highcharts_beschrijving_van_de_grafiek', $p->ID) ?></div>
    												<?php endif; ?>
    
    										<div class="highchart chart-<?php echo $p->ID; ?> <?php the_field('highcharts_hoogte_grafiek', $p->ID) ?>">
    
    a lot of html goes here... 
    
    </div>
    
    <?php endforeach; ?>
    <?php endif; ?>	
    
    										</div><!-- end .charts -->
    			
    <?php endif; ?>
    <?php endwhile; ?>

    Screenshot of the dashboard

  • Hi @Exelmans Graphics

    I think your issue lies within these lines of code:

    <?php if ( get_sub_field("chart") ) : ?>
    											<?php foreach ( get_sub_field("chart") as $p ) : ?>
    <?php echo get_permalink($p->ID); ?>
    												<?php if( get_field( 'highcharts_beschrijving_van_de_grafiek', $p->ID) ): ?>
    												<div class="description"><?php the_field('highcharts_beschrijving_van_de_grafiek', $p->ID) ?></div>
    												<?php endif; ?>

    The first line is OK (testing to see if your ‘chart’ custom field exists) but the line after is a foreach which makes no sense in this context as the post object in your chart field is a single value.

    If you want to get the data associated with another post using the post object field simply do something like the following:

    <?php if ( get_sub_field("chart") ) : 
    
    	$p = get_sub_field("chart");
    	// Variable now stores the post object for the post chosen by the user
    	
    	//You can now do stuff with that post e.g.
    	$my_other_post = get_field( 'highcharts_beschrijving_van_de_grafiek', $p->ID);
    	
    	//This is example only - I have no idea what this field is or does
    	if($my_other_post){
    		echo $my_other_post;
    	}
    
    endif;// End check for chart field
    ?>
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Get field from post_object in Flexible Content’ is closed to new replies.