• We have a small plugin that adds templates to the page post-type and every template needs to have a Gutenberg block called hero. The hero block has an attribute called hero-type, which can be “Story”, “Primary”, “Secondary” and “Tertiary”.

    Some templates however require a specific hero-type. For example, the news-overview needs to have the “Secondary”. How do we go on about doing this?

    We have done this for our post-types, where we have added for example the “Story” hero-type to the “Story” post-type.

    Here is the code how we created the templates:

    <?php
    
    function website_theme_templates($post_templates, $obj, $post, $post_type) {
    	$CUSTOM_TEMPLATES = [
    		'page' => [
    			'home' => 'Home page',
          'contact' => 'Contact',
          'news-overview' => 'Overview: News',
          'story-overview' => 'Overview: Story'
    		]
    	];
    	return array_merge($post_templates, isset($CUSTOM_TEMPLATES[$post_type]) ? $CUSTOM_TEMPLATES[$post_type] : []);
    }
    
    add_filter( 'theme_templates', 'website_theme_templates', 4, 10 );
    • This topic was modified 5 years, 1 month ago by fl0risdg.
    • This topic was modified 5 years, 1 month ago by fl0risdg.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Set Gutenberg block variable from template’ is closed to new replies.