Skip to content

Instantly share code, notes, and snippets.

@bob-moore
Last active September 12, 2017 17:51
Show Gist options
  • Save bob-moore/c28756bfdfd86683f006fb09868d77b0 to your computer and use it in GitHub Desktop.
Save bob-moore/c28756bfdfd86683f006fb09868d77b0 to your computer and use it in GitHub Desktop.
Quick ACF page section stuff
<?php
function display_acf_page_sections() {
// Construct ID
$id = !empty( get_sub_field('section_id') ) ? sprintf( ' id="%s"', esc_attr( get_sub_field('section_id') ) ) : '';
// Construct class
$class = !empty( get_sub_field('section_class') ) ? sprintf( ' class="my_custom_class %s"', esc_attr( get_sub_field('section_class') ) ) : ' class="my_custom_class';
// Construct style
$bg_image = get_sub_field('section_background_image');
$bg_color = get_sub_field('section_background_image');
if( empty( $bg_image ) && empty( $bg_color ) ) {
$style = '';
} elseif( !empty( $bg_image ) ) {
$style = sprintf( ' style="background-image: url(%s)"', esc_url_raw( $bg_image ) );
} elseif( !empty( $bg_color ) ) {
$style = sprintf( ' style="background-color: %s"', sanitize_hex_color( $bg_color ) );
}
include get_stylesheet_directory() . '/partials/page_section.php';
}
add_action( 'display_acf_page_sections' );
// Add another or reuse logic for sub fields
<section <?php echo $id . $class . $style; ?>>
<div class="sub_section_block_container row">
<?php if( have_rows('section_block') ) : while( have_rows('section_block') ): the_row(); ?>
<?php do_action( 'display_sub_field' ); ?>
<?php endwhile; endif; ?>
</div>
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment