jeudi 17 juin 2021

ACF selection field to load template parts not working

I am working on a little function for a Divider.

The Divider is made with Gutenberg in combination with ACF.

When someone loads a Divider they will get a select dropdown from ACF. In this select dropdown they can select a Divider style. For example:

  1. Waves
  2. Triangle
  3. Arrow

These Divider styles are placed inside the Gutenberg block folder Divider. Inside this folder the following files are included:

  1. divider.php
  2. divider.scss
  3. divider.js
  4. divider-gutenberg.php
  5. divider.css

Also there is a folder inside the Divider folder:

  1. divider-parts

Inside this folder the following files are included:

  1. divider-waves.php
  2. divider-triangle.php
  3. divider-arrow.php

When someone selects a Divider style from the dropdown i want to load a divider-part.

But for some reason the divider-part is not loading.

This is the code that i am using inside the Gutenberg block:

        <?php
/**
 * Header Image Template.
 *
 * @param   array $block The block settings and attributes.
 * @param   string $content The block inner HTML (empty).
 * @param   bool $is_preview True during AJAX preview.
 * @param   (int|string) $post_id The post ID this block is saved to.
 */

// Create id attribute allowing for custom "anchor" value.
$id = 'divider-' . $block['id'];
if( !empty($block['anchor']) ) {
    $id = $block['anchor'];
}

// Create class attribute allowing for custom "className" and "align" values.
$className = 'divider';
if( !empty($block['className']) ) {
    $className .= ' ' . $block['className'];
}
// if( !empty($block['align']) ) {
//     $className .= ' align' . $block['align'];
// }
if( $is_preview ) {
  $className .= ' is-admin';
}
?>

<section id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?> container-fluid" style="<?php echo advanced_acf_settings(); ?>">
  <div class="block-background">
      <?php echo block_background(); ?>
  </div>  

  <?php if( get_field('select_divider_style') == the_field('select_divider_style') ): ?>
  
      <?php echo get_template_part( 'divider-parts/divider-', the_field('select_divider_style') ); ?>

  <?php endif; ?>
</section>

Could someone please help me with this problem?

Aucun commentaire:

Enregistrer un commentaire