Created
December 28, 2015 18:37
-
-
Save jrstaatsiii/9b24e654041adc31e24c to your computer and use it in GitHub Desktop.
Get First Instance of Content Block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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') ) | |
return; | |
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]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment