Advanced Custom Fields (ACF) is an extremely useful - and free - WordPress plugin that allows you create complex custom fields, group them and automatically apply them your blog posts. This allows you to add extra Meta data to your blog posts that can be accessed via your Magento templates.
ACF can also be used to create image sliders, add more images to posts or any other complex elements that you can think of!
Have Your Say...
If you have used Advanced Custom Fields and want to share your opinion, you can leave a review by clicking here.
- Game ChangerThis extension allowed my Magento site to be fully customizable with the more user friendly Wordpress interface. It likely ended up saving us hundreds of hours of work due to my expertise in Wordpress and total lack thereof in Magento. Ben has been extremely helpful in terms of support as well.
A must have for the Fishpig extension!
- NikolaiAgain - a must have extension! This allows you to super easily have image galleries etc, super easy to code and extend the blog.
- EduardoHi the extension do exactly what it said. I am an experienced developer and i start generating the functionality by my self but it would going to take me more time than the price of the addon.
I used for a customer of mine who wanted to add a gallery with ajax with several images and not only one fetured image. I also needed to have the post with other images intact so i coudn't get it from the content.
Super easy to install only be sure you have the last fishpig magento wordpress integration instalation.
- BrianI had this up and running in a few minutes with no problems at all. I found it very useful for giving clients the ability to edit content with a complicated visual design that would totally break in a WYIWYG editor.
- LJ ScottI've been bugging Ben about creating this extension for a while and over the moon that he has finally completed it (sorry for all the pestering but am leaving the review as promised!!)
It works great and allows me to increase the complexity of my blog so it is no longer a standard text list style blog. I now have advanced custom data that my blog copywriters can assign to each post and my templates pick this up and display it perfectly.
Who should get this?
If you happen to be using a lot of custom fields in your articles and are tired of having to recreate them each time, advanced custom fields allows you to create groups of custom fields and assign them to posts based on rules. Each time you go to a post in the Wordpress Admin these fields are already listed for you to just enter the content in directly.
Also recommend this extension for people wanting more than simply custom fields and want to include images, post relationships, dates and other complex types
Before installing Advanced Custom Fields, ensure you have the latest version of Magento WordPress Integration and that your blog is integrated into your Magento theme.
You can install Advanced Custom Fields in the same way that you can install any Magento extension. For detailed instructions, please the see the Magento extension installation guide.
This extension will only work if you have installed the free Advanced Custom Fields plugin for WordPress in the WordPress Admin.
Once you have setup your fields in the WordPress Admin and populated some posts with data, you're probably wondering how to actually access that data in your Magento templates. Fortunately, this is the easy part!
Magento WordPress Integration provides a method on the Post model named getMetaValue(string metaKey). By default this method searches the post meta table in WordPress for a value that has a meta key matching the first parameter of the method. The ACF extension uses an event to alter this method so that if an ACF field exists for the post with this key, this value is returned.
Get an ACF Value for a Post
Here is an example of how to get a ACF field that has a field name of 'my_acf_field'.
<?php $value = $post->getMetaValue('my_acf_field') ?>
This works for all field types available in ACF, including the repeater field. When a repeater field is used, the above method returns an array containing all rows and fields inside each row.
Post Collections and ACF
You can also add meta fields to your post collections, filter the collection by a meta value or even sort the collection by a meta value.
addMetaFieldToSelect($key): This method can be called on a post or page collection and will add the meta value to the post/page objects that are created from the collection.
addMetaFieldToFilter($key, $filter): This method will allow you to filter a post/page collection by a meta field value. $key should contain the ACF field key and $filter should contain the filter. This variable is passed to the standard Magento addFieldToFilter method meaning you can use the filter values that you are already used to using.
addMetaFieldToSort($field, $dir = 'asc'): This method allows you to order the post/page collection by a meta field.
Putting all of this together, you can use the following code:
<?php $posts = Mage::getResourceModel('wordpress/post_collection') ->addMetaFieldToSelect('acf_featured_image') ->addMetaFieldToFilter('acf_is_featured', 1) ->addMetaFieldToSort('acf_featured_position', 'ASC') ->load() ?> <?php if (count($posts) > 0): ?> <ul> <?php foreach($posts as $post): ?> <li class="item"> <?php /** Loop through your featued posts * This could be used to create an image slider * of your featured posts **/ ?> </li> <?php endforeach; ?> </ul> <?php endif; ?>
Get an ACF Options Value
The ACF extension now supports the custom options feature of Advanced Custom FIelds. This allows you to specify ACF fields that aren't associated with any particular post, but instead, are global to the blog. These values are like static blocks in Magento, except easy to use and actually useful!
The below code demonstrates how to get an ACF option with a field name of my_acf_option.
<?php $value = Mage::helper('wp_acf')->getOptionValue('my_acf_option') ?>