Add ACF Taxonomy Fields to the Divi Blog Module Post Meta

|

Boosting your Divi Blog Module with Advanced Custom Fields (ACF) can be a powerful way to enhance your posts' metadata. In this tutorial, we'll walk through adding ACF taxonomy fields to your Divi Blog Module post Meta.

Step 1: Enable custom blog module filters

This code relies on the custom dbc_blog_module_post_meta_items filter to modify the post's metadata. For details on how to enable this filter on your blog module, please see the following post:

Step 2: Add the ACF Taxonomy via the Filter

Firstly, add the following PHP code to your site:

// Add filter to show ACF taxonomy in blog module post meta

add_filter('dbc_blog_module_post_meta_items', 'dbc_blog_module_article_post_meta_acf_taxonomies', 10, 3);

function dbc_blog_module_article_post_meta_acf_taxonomies($items, $props, $post_id) {
	if (is_array($items) && $post_id) {
		if (isset($props['module_class']) && is_string($props['module_class']) && (strpos($props['module_class'], 'blog-with-acf-taxonomy') !== false)) {
			$terms = function_exists('get_field')?get_field('taxonomy_field', $post_id):array();
			if (empty($terms)) { return $after; }
			$acf_taxonomy = '';
			foreach ($terms as $term) {
				$term_link = get_term_link($term, $term->taxonomy);
				if (!is_wp_error($term_link)) {
					$acf_taxonomy .= '<a href="' . esc_url($term_link) . '">' . esc_html($term->name) . '</a>, ';
				}
			}
			$items[] = rtrim($acf_taxonomy, ', '); // Remove extra comma at the end.
		}
	}
	return $items;
}

Step 3: Add the CSS Class to the Divi Blog Module

The code above is set up to only apply to Blog Modules with "blog-with-acf-taxonomy" in the CSS Class field (located in the Advanced Tab of the Module settings). 

Once you reach this point, you should hopefully have successfully added an ACF taxonomy field to your Divi Blog Module post meta!

0 Comments

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

Your email address will not be published. Required fields are marked *.

We may earn a commission when you visit links on our website.