WordPress + ACF Snippet
Last active
July 7, 2017 21:27
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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