Skip to content

Snippet: Add New “Blocks” to the Portfolio Post Layout

The following snippet will show you how to add a custom block to your portfolio layout which you can enable in the Customizer and then control the output via a callback or anonymous function, however, did you know you can build custom templates via the WordPress admin?

/**
 * Registers a new block for the portfolio posts.
 *
 * @link https://total.wpexplorer.com/docs/snippets/add-new-portfolio-single-blocks/
 */
add_filter( 'totaltheme/portfolio/single_blocks/choices', function( $blocks ) {
    $blocks['tags'] = esc_html__( 'Tags', 'text_domain' );
    return $blocks;
}, 10, 2 );

/**
 * Render our custom portfolio post blocks.
 *
 * @link https://total.wpexplorer.com/docs/snippets/add-new-portfolio-single-blocks/
 */
add_filter( 'totaltheme/portfolio/single_blocks', function( $blocks ) {
    if ( isset( $blocks['tags'] ) ) {
        $blocks['tags'] = function() {
            $taxonomy   = 'portfolio_tag';
            $show_links = true;
            $echo       = false;
            $tags       = wpex_list_post_terms( $taxonomy, $show_links, $echo );
            if ( $tags ) {
                echo '<div class="my-portfolio-tags">' . $tags . '</div>';
            }
        };
    }
    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