skip to Main Content

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( 'wpex_portfolio_single_blocks_choices', function( $blocks ) {
    $blocks['tags'] = __( 'Tags', 'textdomain' );
    return $blocks;
}, 10, 2 );

/**
 * Render our custom portfolio post blocks.
 *
 * @link https://total.wpexplorer.com/docs/snippets/add-new-portfolio-single-blocks/
 */
add_filter( 'wpex_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 the Code Snippets Plugin (or alternative plugin)
Back To Top