Skip to content

Snippet: Add/Remove “Blocks” From Custom Post Type Entries

Instead of creating custom entry blocks you may want to look at creating a Custom Card design instead, it's easier and more awesome. Also, if you are using the Post Types Unlimited plugin you can control which blocks display via the plugin settings.

The following snippet shows how you can use the 'wpex_{post_type}_entry_blocks' filter to add or remove sections/blocks from your custom post type entries which display on Archives.

/**
 * Any custom post type post entry can be altered via the wpex_{post_type}_entry_blocks filter
 * which returns an array of the "blocks" or parts for the layout
 * that will be loaded from partials/cpt/cpt-entry-{block}.php
 *
 * Default array: array( 'media', 'title', 'meta', 'content', 'readmore' );
 *
 * You can use the filter to add, remove or re-order elements in the array as you wish.
 * Below is an example showing how to add a new block for a custom post type and remove one.
 *
 * After adding new blocks simply create a new file with the name "cpt-entry-YOUR_KEY.php"
 * and place this file at partials/cpt/ in your child theme.
 *
 * IMPORTANT: Make sure to change {post_type} with your post type name
 * IMPORTANT: Make sure to replace "YOUR_KEY" with the key given in the array (advertisement is the key for the block added below)
 *
 * @link https://total.wpexplorer.com/docs/snippets/cpt-entry-blocks/
 */
add_filter( 'wpex_{post_type}_entry_blocks', function( $blocks ) {

    // Example 1 - Add new block "advertisement" ( requires template part for output )
    $blocks['advertisement'] = __( 'My Advertisement', 'total' );

    // Example 2 - Added in Total 4.0+
    // Add new blocks with custom function output
    // Below is an example with an anonymous function but you can use custom functions as well ;)
    $blocks['custom_block'] = function() {
    	echo 'my custom block';
    };

    // Example 3 - Remove the "media block".
    unset( $blocks['media'] );

    return $blocks;
} );
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