Magento WordPress ACF Code Examples

« Back to Magento WordPress ACF

  • Magento 1
  • Magento 2
Magento 1

ACF Post Custom Fields

The most common field types in ACF are assigned to posts. Assuming you have $post, you can call the getMetaVaule($fieldKey) method on it to get an ACF value. This works for posts of any post type, including pages.

The code below gets an ACF field value for $post with the field name 'my_acf_field' and echo's it out.

<?php echo $post->getMetaValue('my_acf_field') ?>

If you don't have $post assigned in the template you are working in but the current request is a post page, you can load $post from the registry.

<?php
	
	if ($post = Mage::registry('wordpress_post')) {
		echo $post->getMetaValue('my_acf_field');
	}

?>

If you're not on a post page, you can load a new $post object.

<?php
	$post = Mage::getModel('wordpress/post')->load(1234);
	
	if ($post->getId()) {
		echo $post->getMetaValue('my_acf_field');
	}
	
?>

ACF Options Value

Option fields aren't assigned to a specific post and are a great way to add config style data to your site.

<?php echo Mage::helper('wp_addon_acf')->getOptionValue('my_acf_option') ?>

ACF Term/Taxonomy Custom Fields

The below code demonstrates how to get an ACF value for a term. This can be used on terms that belong to the default taxonomies and custom taxonomies (if you have the Post Types and Taxonomies add-on extension installed).

<?php echo $term->getMetaValue('my_acf_field') ?>

Just like with posts, you can get the $term object from the registry if the current page is a term page.

<?php
	
	if ($term = Mage::registry('wordpress_term')) {
		echo $term->getMetaValue('my_acf_field');
	}

?>

You can also load a new $term.

<?php
	$term = Mage::getModel('wordpress/term')->load(1234);
	
	if ($term->getId()) {
		echo $term->getMetaValue('my_acf_field');
	}
	
?>

ACF Widget Fields

ACF now allows you to add fields to widgets. If you're in a widget template, it's really easy to access this data.

<?php
// Get the ACF Helper
$acfHelper = Mage::helper('wp_addon_acf');

// Get the scope. As an example, this would be archives-3
$widgetScope = $this->getWidgetType() . '-' . $this->getWidgetId();

// Print the widget value to the screen
echo $acfHelper->getWidgetValue('widgets_image', widgetScope);

?>
Magento 2

ACF Post Fields

/*
 * Assuming $post is defined
 */
echo $post->getMetaValue('my_acf_field');

/*
 * $post is not defined in template but 
 * current request is a post page so we can
 * load $post from registry
 */
$registry = \Magento\Framework\App\ObjectManager::getInstance()
	->get('Magento\Framework\Registry');

if ($post = $registry->registry('wordpress_post')) {
	echo $post->getMetaValue('my_acf_field');
}

/*
 * $post is not defined in template and current page 
 * is not a post page so load new $post object
 */
$post = \Magento\Framework\App\ObjectManager::getInstance()
	->get('FishPig\WordPress\Model\PostFactory')->create();

if ($post->load(1234)->getId()) {
	echo $post->getMetaValue('my_acf_field'])	;
}

ACF Term Fields

/*
 * Assuming $term is defined
 */
echo $term->getMetaValue('my_acf_field');

/*
 * $post is not defined in template but 
 * current request is a post page so we can
 * load $post from registry
 */
$registry = \Magento\Framework\App\ObjectManager::getInstance()
	->get('Magento\Framework\Registry');

if ($term = $registry->registry('wordpress_term')) {
	echo $term->getMetaValue('my_acf_field');
}

/*
 * $post is not defined in template and current page 
 * is not a post page so load new $post object
 */
$term = \Magento\Framework\App\ObjectManager::getInstance()
	->get('FishPig\WordPress\Model\TermFactory')->create();

if ($term->load(1)->getId()) {
	echo $term->getMetaValue('my_acf_field'])	;
}

ACF Option Fields

/*
 * We'll use the Object Manager for simplicity but you can use
 * constructor injection to inject the Helper class
 */
$acfHelper = \Magento\Framework\App\ObjectManager::getInstance()
	->get('FishPig\WordPress_ACF\Helper\Data');

// Echo out the options field value
echo $acfHelper->getOptionsField('my_acf_options_field');

ACF Widget Fields

/*
 * We'll use the Object Manager for simplicity but you can use
 * constructor injection to inject the Helper class
 */
$acfHelper = \Magento\Framework\App\ObjectManager::getInstance()
	->get('FishPig\WordPress_ACF\Helper\Data');

// Echo out the options field value
echo $acfHelper->getWidgetField('my_widget_field', 'archives-3');

// If you are an in a widget template (FishPig_WordPress::sidebar/widget/*.phtml) you can use:
echo $acfHelper->getWidgetField(
  'my_widget_field', 
  $this->getWidgetType() . '-' . $this->getWidgetId()
 );

Magento WordPress
ACF
Documentation

Post your comment

FishPig Ltd