Skip to content

Snippet: Add/Remove “Blocks” (Sections) From Custom Post Type Posts

/**
 * Any custom post type post layout can be altered via the totaltheme/cpt/single_blocks filter
 * which returns an array of the "blocks" for the layout.
 *
 * The default $blocks array is: array( 'media', 'title', 'meta', 'content', 'page-links', 'share', 'comments' );
 *
 * You can use the filter to add, remove or re-order elements in the array as you wish.
 */

add_filter( 'totaltheme/cpt/single_blocks', function( $blocks, $post_type ) {

    // Only modify the my_custom_post_type post type.
    if ( 'my_custom_post_type' !== $post_type ) {
        return;
    }

    /** Adding blocks ***/

        // Option 1: Add new block "advertisement" and create a new file at partials/cpt/cpt-single-advertisement.php with your output.
        $blocks['advertisement'] = 'advertisement';

        // Option 2: Add a new block with an anonymous function and inline output.
        $blocks['block_id'] = function() {
            echo 'my custom block';
        };

        // Option 3: Add a new block that loads a custom file.
        $blocks['block_id'] = function() {
            get_template_part( 'some-folder/file-name' );
        };

    /*** Removing blocks with known keys ***/

        // Remove the default media block.
        unset( $blocks['media'] );

    // Return blocks
    return $blocks;

}, 10, 2 );
All PHP snippets should be added via child theme's functions.php file or via a plugin. We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top