Support » Developing with WordPress » Is there any way to create a block template for a single page or page template?

  • coleh

    (@coleh)


    I have been trying to create a block template but only want it to show up on the “Home” page for the website. I have not found any way to make this work as I cannot access anything but the post ID in the “init” hook and this theme runs on multiple websites with different post IDs for the page called “Home”

    I am looking for ways to somehow make this work so when a user goes to edit the “Home” page under Pages, they are shown the block template that cannot be removed or re-arranged.

    function create_homepage_block_template()
    {
        if (!is_admin() || !is_page('Home')) {
            return false;
        }
    
        $post_type_object = get_post_type_object('page');
        $post_type_object->template = array(
            array('homepage/featured-content', array(
                'align' => 'full',
            )),
            array('homepage/featured-modules', array(
                'align' => 'full',
            )),
            array('homepage/posts-box', array(
                'align' => 'full',
            )),
        );
        $post_type_object->template_lock = 'all';
    }
    add_action('init', 'create_homepage_block_template');
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Blocks don’t make for very good templates. They’re meant to be fully editable and rearranged. IMO you’re better off building a custom template file which is populated by a series of custom fields. Each field’s content will only be applied to a particular element on the template, severely limiting the user’s ability to alter the page layout.

    A template file named “front-page.php” will always be used for the home page regardless of which WP page is assigned as home page. There are a number of custom fields plugins you can use. Depending on the plugin, it should be possible to only use custom fields on the home page. All other pages are edited as normal.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.