Skip to content

Instantly share code, notes, and snippets.

@ControlledChaos
Last active July 7, 2017 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ControlledChaos/fe7a854795c64da0cc823e489c63b5dc to your computer and use it in GitHub Desktop.
Save ControlledChaos/fe7a854795c64da0cc823e489c63b5dc to your computer and use it in GitHub Desktop.
Template for adding ACF fields or other content to post types. Filters the the_content() function.

Post Type Content Filter

WordPress + ACF Snippet

<?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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment