Home › Forums › ACF PRO › Get first of layout 'type' in Flexible Content › Reply To: Get first of layout 'type' in Flexible Content
Hi @jrstaatsiii,
Thank you for sharing the solution. I will now copy the same to this forum so that it can help out a fellow dev in future:
<?php
/*
* Returns the first instance of a given layout option
* @param - $id - the id of the post you are trying to target: '' by default
* @param - $fc_field - the name of the ACF flexible field: 'content_blocks' as the default
* @param - $fc_layout - the name of the flexible content layout: 'visual_editor' as the default
* @return - mixed
* @todo - test different types of returned content. at the moment, I am only using this for returning a string
*/
function get_first_instance_of_content_block( $id = '', $fc_field = 'content_blocks', $fc_layout = 'visual_editor' ) {
if ( class_exists('acf') ) {
if ( have_rows( $fc_field, $id ) ) {
$content_blocks = get_field( $fc_field, $id );
$content = array();
foreach ( $content_blocks as $block ) {
if ( $block['acf_fc_layout'] == $fc_layout ) {
$content[] = $block[$fc_layout];
}
}
reset($content);
return $content[0];
}
} else {
return '<p class="error">Advanced Custom Fields is required for <code>get_first_instance_of_content_block()</code> to work.</p>';
}
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.