Skip to content

Instantly share code, notes, and snippets.

@Kpudlo
Last active October 2, 2023 20:32
Show Gist options
  • Save Kpudlo/e0368eb78601e4ea78c162171ad52b36 to your computer and use it in GitHub Desktop.
Save Kpudlo/e0368eb78601e4ea78c162171ad52b36 to your computer and use it in GitHub Desktop.

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 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. image

Code Snippet:

<?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 '';
}
?>
@MarkusSt88
Copy link

Hello and thanks for the super code, it works fine.
But for images this code can not be used or?
Can this code be adapted so that it also works for background images of a section or do I then need a whole new code here?

@Kpudlo
Copy link
Author

Kpudlo commented Mar 27, 2023

Hello and thanks for the super code, it works fine. But for images this code can not be used or? Can this code be adapted so that it also works for background images of a section or do I then need a whole new code here?

The code is updated to include all field types in Oxygen.

@KittenCodes
Copy link

This is great, thanks @Kpudlo.

@gagankhatri
Copy link

@Kpudlo this is a great snippet, loved it. Thank you for the share.
If I am not mistaken, I find that this doesn't support/work with ACF Repeater fields.
Looking forward to having an updated snippet to support all the pro elements of ACF. Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment