This is my very basic guide to using the Advanced Custom Fields plugin in WordPress. Advanced Custom Fields allows you to add extra parameters and images to pages and blog posts in WordPress. For example you can use it to add an extra text field in the backend that then displays that value on a blog post or product page if you’re using WooCommerce.
Adding a new text field to a blog post using ACF in WordPress
1. Create a new text field in the ACF Plugin
2. Add some code to your template to display the created field
3. For example the created field is – ‘item-title’
4. To display in a post or page use the code below
<?php the_field('item_title'); ?>
Adding a image to a blog post using ACF in WordPress
1. Create a new image field in the ACF Plugin
2. Add some code to your template to display the created field
3. For example the created field is – ‘feature_image’
4. To display in a post or page use the code below
<?php if( get_field('feature_image') ): ?> <img src="<?php the_field('feature_image'); ?>" /><?php endif; ?>
Adding fields to a Taxonomy/Tag page using ACF in WordPress
To add ACF or a custom field to a Taxonomy/Tag page you need to add an extra property to get this to work.
1. Create new text field(s) in the ACF Plugin
2. For ACF to show on the Tag edit page use: Location: Taxonomy – is equal to – All
3. Add some code to your template to display the created field
4. In this example the created field(s) are ‘link_url’ and ‘link_name’
5. To display in a tag archive use the code below
<?php $queried_object = get_queried_object(); ?>
At the end of the field you add ‘, $queried_object‘
<a href="<?php bloginfo('url'); ?>/<?php the_field('link_url', $queried_object); ?>"> <?php the_field('link_name', $queried_object); ?></a>
Bringing values through from ACF into WordPress template file
If you wanted to bring values through from ACF, eg. Category number or number of posts to display etc.
Use the following:
1. Create the text fields in ACF
2. For example: ‘category_number’ and ‘number_of_posts’
3. Define the values using ‘$term’ and ‘`$variable’
4. Add actions to the values above
$term = get_field('category_number'); $variable = get_field('number_of_posts');
The action bit, eg. where it’s used would look like
$args = array( 'posts_per_page' => $variable, 'offset'=> 0, 'category' => $term );