OGKDev
10/10/2019 - 12:18 AM

OGK Section Structure

<?php
//This is the main function to get a section. 
//It will search for the section, firstly in the activated theme (child theme) and default to the one found in the parent theme (if a parent theme one exists). 
function ogk_get_section($section, $data, $config = array()) {

  // Grab the template part from the child theme first or parent theme last
  // Using include instead of get_template_part() allows the variables to be used in the regular way
  include(locate_template('sections/' . $section . 'php', false));

}
<?php
// located in the theme (parent or child) at /sections/boxes.php
// This file (boxes.php) would be in the parent theme and you if you need to make changes to it, you can simply copy the file to the sections directory in your child theme and it will have priority
if ( $data ): ?>
    <div class="boxes">
      <?php foreach ($data as $box) : ?>
          <div class="box">
            <?php $link = $box['box_link']; ?>
              <a href="<?= $link['url'] ?>" title="<?= $link['title'] ?>" target="<?= $link['target'] ?>">
                <?php if ( $box['icon'] ): ?>

                  <?php 
                  // Show the icon only if the specified in the options
                  if ( $config['show_icon'] ) :
                    echo file_get_contents($box['icon']);
                  endif; 
                  ?>

                <?php endif; ?>
                  <p><strong><?= $box['box_heading']; ?></strong></p>
                  <p><?= $box['text']; ?></p>
              </a>
          </div>
      <?php endforeach; ?>
    </div>
<?php endif; ?>
<?php
// Located in a theme template file
// This is where you grab the data and set some config options
// This uses a ACF repeater's data
if ( have_rows('boxes') ) :
  
  $data = array();
  $config = array();

  while (have_rows('boxes')): the_row();

    $data[] = array(
      'box_link' => get_sub_field('box_link'),
      'icon' => get_sub_field('icon'),
      'box_heading'] = get_sub_field('box_heading'),
      'icon' => get_sub_field('icon'),
      'text' => get_sub_field('text')
      );

  endwhile;

  $config['show_icon'] = true);

  ogk_get_section('boxes', $data, $config);

endif;

?>
<?php
// Located in a theme template file
// This is where you grab the data and set some config options
// This uses a ACF repeater's data
if ( have_rows('boxes') ) :
 
  $data = array (
    'boxes' => get_field('boxes')
    )
    
  $config = array(
    'show_icon' => true
  );
 
  ogk_get_section('boxes', $data, $config);
 
endif;
 
?>