ControlledChaos of Controlled Chaos Design
7/20/2017 - 4:32 PM

Template for adding ACF fields or other content to post types. Filters the the_content() function.

Template for adding ACF fields or other content to post types. Filters the the_content() function.

<?php

/**
 * Post type content filter for ACF fields
 *
 * @link       http://example.com/
 * @since      1.0.0
 *
 * @package    Controlled Chaos
 * @subpackage Controlled_Chaos/post-types/my-cpt
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

if ( class_exists( 'acf' ) && ! function_exists( 'ccd_field_content' ) ) :
	function ccd_field_content() {
	  // HTML to output on gallery posts
		ob_start();
			// Fields here
		return ob_get_clean();
	}

	function ccd_field_content_filter( $content ) {
	  // Filter the_content to insert the above HTML
		$fields = ccd_field_content();

		if ( ( is_singular( 'my_cpt' ) && in_the_loop() && is_main_query() ) || ( is_post_type_archive( 'my_cpt' ) && in_the_loop() && is_main_query() ) ) {
			return $fields;
		}
		return $content;
	}
	add_filter( 'the_content', 'ccd_field_content_filter', 10, 2 );
endif;

Post Type Content Filter

WordPress + ACF Snippet