Note: This feature is added in WordPress 6.1. Now, it is possible to create block-based template parts in classic themes and allows the same flexibility same as block-based theme.
Add block template part support in the Classic theme
To enable block template parts use the after_setup_theme
action hook which is the correct hook to use when enabling theme functionality. Once this is done a new menu item for template parts will appear under the appearance menu
in the WordPress dashboard.
Add these code snippets in the functions.php file of your theme.
add_action( 'after_setup_theme', 'wp_add_block_template_part_support' ); function wp_add_block_template_part_support() { add_theme_support( 'block-template-parts' ); }
The next step is to create a directory called /parts in the theme’s root directory and create your block template parts inside this directory. Create a footer.html in /parts directory.
<!-- wp:group {"layout":{"inherit":true}} --> <div class="wp-block-group"> <!-- wp:group {"style":{"spacing":{"padding":{"top":"80px","bottom":"30px"}}}} --> <div class="wp-block-group" style="padding-top:80px;padding-bottom:30px"> <!-- wp:paragraph {"align":"center"} --> <p class="has-text-align-center">Proudly Powered by <a href="https://kishanjasani.wordpress.com/" rel="nofollow">Kishan Jasani</a></p> <!-- /wp:paragraph --> </div> <!-- /wp:group --> </div> <!-- /wp:group -->
Once the file is created you can refresh the template editor, and you will see footer template part is available in the list. WordPress automatically reads any .html files in the parts directory of a theme and makes them available as template Parts in the editor.
Notice you can’t add template parts from the editor they have to be created as files in the theme to use your template part in the classic theme. You can include template_part using the block_template_part function. This function accepts one argument which is a template filename without a .html extension. so here for our footer.html template part, we can add a footer argument like this:
block_template_part( 'footer' );
Use this function in your footer.php template file. and check your site footer will fetch from block template parts.
Happy Coding!😍
Leave a Reply