Support

Account

Home Forums Add-ons Flexible Content Field Custom post type value in flexible content

Solving

Custom post type value in flexible content

  • Hi,

    In my wordpress installation I created a Flexible Content field called flexible_homepage_banners. This acf field has two layouts: banners_small and banner_large.
    Banners_small is a repeater which looks for a custom post type called banner: banner_large also looks at this custom post type.

    The code I put in my template is as follows:

    <?php if(have_rows('flexible_homepage_banners')) : ?>
    			<?php while(have_rows('flexible_homepage_banners')) : the_row(); ?>
    			
    				<?php if(get_row_layout() == 'banners_small') : ?>
    					
    					<?php if(have_rows('banners_small')) : ?>
    						
    						<div class="banners__small">
    						
    							<?php while(have_rows('banners_small')) : the_row(); ?>
    							
    								<div class="banner banner--small">
    									kleine banner
    								</div>
    							
    							<?php endwhile; wp_reset_query(); ?>
    						
    						</div>
    						
    					<?php endif; ?>
    				
    				<?php endif; ?>
    				
    				<?php if(get_row_layout() == 'banner_large') : ?>
    				
    					<div class="banner banner--large">
    						grote banner
    					</div>
    				
    				<?php endif; ?>
    			<?php endwhile; wp_reset_query(); ?>
    		<?php endif; ?>

    This works great; the correct amount of banners I select on the page is getting returned; so far so good.

    But here’s the tricky part, well tricky for me at least…

    How do I echo the content of those banners? Things like the_title returns the page title; not the title of the banner post type.

    I’m missing something simple, I think…

  • I’ve seem to fixed the issue by replacing the post object (custom post type) with a normal repeater.
    I think it’s still possible to use post objects in combination with the flexible content field, but for now my problem has been solved.

  • Hmz, sorry for the spam, but after some rethinking, my earlier reply did not in fact solve my problem. I do need to use the custom post type in my flexible content field…

    So back to my initial question; how do I get custom post type data like title, content and excerpt through a flexible content field?

  • You need to tell WP where to get the information from.

    To be honest, I can’t tell by your code above exactly what you mean. But there is an explanation on getting fields from the related post on the post object field doc https://www.advancedcustomfields.com/resources/post-object/

    You can either do this or you can supply the post ID to the WP functions.

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

The topic ‘Custom post type value in flexible content’ is closed to new replies.