Skip to content

Instantly share code, notes, and snippets.

@kreativan
Created February 12, 2024 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kreativan/b5c87668cd0b9e3c71963993321ca958 to your computer and use it in GitHub Desktop.
Save kreativan/b5c87668cd0b9e3c71963993321ca958 to your computer and use it in GitHub Desktop.
acf-page-builder.php
<?php
/**
* Page Builder
* @example tpf_include('html/page-builder', $args);
* @param string $folder - folder path from where to include blocks
* @param string $container - container class
* @param string $display - default display option, should be a class that defines space and style
* @param array $layout_settings - individual layout settings
*
$args = [
// Flexible type field name
'name' => 'page_builder',
// Folder to pull pb items from
'folder' => 'page-builder',
// Container class
// set "false" to skip the container
// useful when you want to wrap whole page builder in a container
'container' => "uk-container uk-container-small",
// Default style option
// Should be a class that defines space and style
'style' => 'uk-margin-large',
// Layout (element) specific settings
// override style class
// set style to "false" or "disabled" to hide it
// override container class
// add custom class
'gallery' => [
'style' => 'uk-section uk-section-muted',
'container' => 'uk-container uk-container-small',
'class' => 'my-custom-class',
],
];
*
*/
$field_name = isset($args['name']) ? $args['name'] : "page_builder";
$post_id = isset($args['post_id']) ? $args['post_id'] : null;
$page_builder = get_field($field_name, $post_id);
$folder = !empty($args['folder']) ? $args['folder'] : "page-builder";
$container = !empty($args['container']) ? $args['container'] : "uk-container";
$style = !empty($args['style']) ? $args['style'] : "uk-margin-large";
$l = 0;
$i = 1;
?>
<?php while (have_rows($field_name)) :
the_row();
$layout = get_row_layout();
// item css class start
$class = "pb-item pb-$layout";
// Set default item container
$item_container = $container;
// Style field
$style_field = !empty(get_sub_field('pb_style')) ? get_sub_field('pb_style') : get_sub_field('style');
$style_field = is_array($style_field) ? $style_field['value'] : $style_field;
$pb_style = !empty($style_field) ? $style_field : $style;
if ($style_field == "default" || $style_field == "style-default") $pb_style = $style;
// Is item disabled?
$is_disabled = ($pb_style == "false" || $pb_style == "disabled") ? true : false;
// is there custom layout (element) settings passed to the file?
$layout_settings = isset($args[$layout]) ? $args[$layout] : false;
// If there is layout specific settings
if ($layout_settings) {
if (isset($layout_settings['style'])) {
// override display class
$pb_style = $layout_settings['style'];
// disabled element if display is false or disabled
if ($pb_style == "false" || $pb_style == "disabled") $is_disabled = true;
};
// override item container
if (isset($layout_settings['container'])) $item_container = $layout_settings['container'];
// add custom class
if (isset($layout_settings['class'])) $class .= " {$layout_settings['class']}";
}
// If item is not disabled
if (!$is_disabled) {
?>
<?php
$n = $i++;
// add display class
$class .= " $pb_style";
// Add first and last class
if ($n == 1) $class .= " pb-first";
if ($n == count($page_builder)) $class .= " pb-last";
?>
<div class="<?= $class ?>">
<?php if ($container != "false" && $pb_style != "no-space") : ?>
<div class="<?= $item_container ?>">
<?php endif; ?>
<?php
$data = get_fields();
$data = $data[$field_name][$l];
get_template_part("{$folder}/{$layout}", null, $data);
wp_reset_postdata();
?>
<?php if ($container != "false" && $pb_style != "no-space") : ?>
</div>
<?php endif; ?>
</div>
<?php } ?>
<?php endwhile; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment