This code will add a new Dynamic Data option to get ACF Archive fields in Oxygen.
- You can add this code to your site by installing your preferred snippets plugin and copy/paste the PHP code below into a new snippet.
- You should then see a new Dynamic Data option called "ACF Archive Field".
- After you select that Dynamic Data field, enter the Field Name (not Label) of the ACF field, and everything should work.
<?php
add_filter( 'oxygen_custom_dynamic_data', 'oxys_acf_archive_data', 10, 1 );
function oxys_acf_archive_data( $dynamic_data ) {
$properties[] = array(
'name' => __('ACF Field Name', 'oxygen-acf-archive'),
'data' => 'acf_name',
'type' => 'text',
);
//Add Content Option
$content_data = array(
'name' => __( 'ACF Archive Field', 'oxygen-acf-archive' ),
'mode' => 'content',
'position' => 'Archive',
'data' => 'acf_archive_field',
'handler' => 'oxys_acf_archive_field',
'properties' => $properties
);
$dynamic_data[] = $content_data;
//Add Image Option
$image_data = array(
'name' => __( 'ACF Archive Field', 'oxygen-acf-archive' ),
'mode' => 'image',
'position' => 'Archive',
'data' => 'acf_archive_field',
'handler' => 'oxys_acf_archive_field',
'properties' => $properties
);
$dynamic_data[] = $image_data;
//Add Link Option
$link_data = array(
'name' => __( 'ACF Archive Field', 'oxygen-acf-archive' ),
'mode' => 'link',
'position' => 'Archive',
'data' => 'acf_archive_field',
'handler' => 'oxys_acf_archive_field',
'properties' => $properties
);
$dynamic_data[] = $link_data;
//Add Custom Field Option
$custom_data = array(
'name' => __( 'ACF Archive Field', 'oxygen-acf-archive' ),
'mode' => 'custom-field',
'position' => 'Archive',
'data' => 'acf_archive_field',
'handler' => 'oxys_acf_archive_field',
'properties' => $properties
);
$dynamic_data[] = $custom_data;
//Add Image ID Option
$image_id_data = array(
'name' => __( 'ACF Archive Field', 'oxygen-acf-archive' ),
'mode' => 'image-id',
'position' => 'Archive',
'data' => 'acf_archive_field',
'handler' => 'oxys_acf_archive_field',
'properties' => $properties
);
$dynamic_data[] = $image_id_data;
return $dynamic_data;
}
function oxys_acf_archive_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 '';
}
?>
This is great, thanks @Kpudlo.