admin管理员组

文章数量:1125102

I'm writing a plugin that provides a custom post type. I am currently trying to create the block templates for the "single" and the "archive" associated with the custom post type via single-slug.html and archive-slug.html files in the plugin. I do not seem to be succeeding.

This method does not seem to work for me so far:

add_filter(
    'get_block_templates',
    function ( $query_result, $query ) {
        if ( isset( $query['theme'] ) && isset( $query['slug__in'] ) && isset( $query['slug__in'][1] ) ) {
            foreach ( $query['slug__in'] as $slug ) {
                $template_file_path = plugin_dir_path( __FILE__ ) . '/includes/templates/' . $slug . '.html';
                if ( file_exists( $template_file_path ) ) {
                    $html = file_get_contents( $template_file_path );
                    array_push(
                        $query_result,
                        (object) array(
                            'title'               => sprintf( __( 'Default %s', 'post-type-slug' ), $slug ),
                            'slug'                => $slug,
                            'status'              => 'publish',
                            'type'                => 'wp_template',
                            'description'         => sprintf( __( 'Default %s template', 'post-type-slug' ), $slug ),
                            'content'             => $html,
                            'source'              => 'plugin',
                            'is_custom'           => false,
                            'is_customized'       => false,
                            'is_reusable'         => false,
                            'is_reserved'         => false,
                            'is_published'        => true,
                            'is_wp_template_part' => false,
                        )
                    );
                }
            }
        }
        return $query_result;
    },
    10,
    2
);

And I am a little stuck on how to continue. Any guidance would be appreciated.

本文标签: Add block templates (html) via plugin for custom post type