ACF Archive Dynamic Data For Oxygen

This code will add a new Dynamic Data option to get ACF Archive fields in Oxygen.

Installation:

  1. You can add this code to your site by installing your preferred snippets plugin, such as WP Code Box, and copy/paste the PHP code below into a new snippet.
  2. You should then see a new Dynamic Data option called “ACF Archive Field”.
  3. After you select that Dynamic Data field, enter the Field Name (not Label) of the ACF field, and everything should work.

function Code

<?php
/**
 * ACF Archive Dynamic Data
 * 
 * @param array $dynamic_data - the ACF field name/slug 
 * @return array $dynamic_data - the results of getting the ACF field
 */
add_filter('oxygen_custom_dynamic_data', 'bitt_acf_archive_data', 10, 1);

if (!function_exists('bitt_acf_archive_data')) {


 function bitt_acf_archive_data($dynamic_data)
 {

  $properties[] = array(
   'name' => __('ACF Field Name', 'oxygen-codebitt'),
   'data' => 'acf_name',
   'type' => 'text',
  );

  //Add Content Option
  $content_data = array(
   'name' => __('ACF Archive Field', 'oxygen-codebitt'),
   'mode' => 'content',
   'position' => 'Archive',
   'data' => 'acf_archive_field',
   'handler' => 'bitt_acf_get_field',
   'properties' => $properties
  );

  $dynamic_data[] = $content_data;

  //Add Image Option
  $image_data = array(
   'name' => __('ACF Archive Field', 'oxygen-codebitt'),
   'mode' => 'image',
   'position' => 'Archive',
   'data' => 'acf_archive_field',
   'handler' => 'bitt_acf_get_field',
   'properties' => $properties
  );

  $dynamic_data[] = $image_data;

  //Add Link Option
  $link_data = array(
   'name' => __('ACF Archive Field', 'oxygen-codebitt'),
   'mode' => 'link',
   'position' => 'Archive',
   'data' => 'acf_archive_field',
   'handler' => 'bitt_acf_get_field',
   'properties' => $properties
  );

  $dynamic_data[] = $link_data;

  //Add Custom Field Option
  $custom_data = array(
   'name' => __('ACF Archive Field', 'oxygen-codebitt'),
   'mode' => 'custom-field',
   'position' => 'Archive',
   'data' => 'acf_archive_field',
   'handler' => 'bitt_acf_get_field',
   'properties' => $properties
  );

  $dynamic_data[] = $custom_data;

  //Add Image ID Option
  $image_id_data = array(
   'name' => __('ACF Archive Field', 'oxygen-codebitt'),
   'mode' => 'image-id',
   'position' => 'Archive',
   'data' => 'acf_archive_field',
   'handler' => 'bitt_acf_get_field',
   'properties' => $properties
  );

  $dynamic_data[] = $image_id_data;

  return $dynamic_data;
 }
}


if (!function_exists('bitt_acf_get_field')) {
 /*
  * ACF Archive Dynamic Data
  * 
  * @param array $atts - the ACF field name/slug 
  * @return array $data - the results of getting the ACF field
  */
 function bitt_acf_get_field($atts)
 {

  // If ACF is deactivated, return nothing
  if (!class_exists('acf')) {
   return '';
  }

  // Get and store the query object
  global $wp_query;
  $oxy_obj = $wp_query->get_queried_object();

  // Check and store the ACF field if the queried object exists. 
  if ($oxy_obj) {
   $data = get_field($atts['acf_name'], $oxy_obj->taxonomy . '_' . $oxy_obj->term_id);
  }

  // If the ACF field has data, display it
  if ($data) {
   return $data;
  }

  // display nothing if we get no results
  return '';
 }
}
?>
Leave a Reply

Your email address will not be published. Required fields are marked *

    Table Of Contents

    Related Content

    © 2020-2024 Luxibay
    Name(Required)
    Please let us know what's on your mind. Have a question for us? Ask away.
    This field is for validation purposes and should be left unchanged.