ブロックテーマは基本的にhtmlで作られていますが、実はphpテンプレートも読み込み可能です。
- 「templates」フォルダ内ではhtmlテンプレートしか認識しない
つまりphpテンプレートはテンプレートフォルダ直下に配置する必要があります。 - phpテンプレートではheader・footerのパーツ類はphpで呼び出す
書き方は後述します。 - header.htmlやfooter.htmlを読み込んだだけでは
wp_head()
/wp_footer()
は呼びだされないwp_head()
/wp_footer()
はPHP内で手動呼び出しが必要
つまり最低限、下記のようなテンプレート構造になります。あくまで最低限ね・・・
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<?php
// header.html の内容を表示(ブロックで作ったやつ)
$header_path = get_theme_file_path( 'parts/header.html' );
if ( file_exists( $header_path ) ) {
echo do_blocks( file_get_contents( $header_path ) );
}
?>
<!-- メインコンテンツ -->
<main>
...
</main>
<?php
// footer.html の内容を表示
$footer_path = get_theme_file_path( 'parts/footer.html' );
if ( file_exists( $footer_path ) ) {
echo do_blocks( file_get_contents( $footer_path ) );
}
?>
<?php wp_footer(); ?>
</body>
</html>
同時に使っているブロックテンプレートと同じレイアウトにしようとすると、結構厄介ですが、どうしてもPHPテンプレートを使いたい場合に、やれる方法がある、と知っていることは制作者心理としては安心です。