snow monkeyでhookを確認する。
Snow Monkeyのdocumentに
https://snow-monkey.2inc.org/manual-advanced/template-hooks/
すべてのテンプレートがこのフックを通ります。テンプレートが決定される直前に通り、テンプレートの
slugやnameはもちろん、テンプレートに渡される変数(vars)も変更できます。
とあるので、
add_filter(
'snow_monkey_get_template_part_args',
function( $args ) {
print_r($args);
return $args;
}
);
とすれば、hookが全て表示される。これを使って、どの箇所を変更したいかを選定する。
例えば、投稿ページはこんな感じになる。

これを見ると、例えばpost singleのcontentsテンプレートを上書きしたい場合は、
Array ( [slug] => template-parts/content/entry/content/content [name] => post [vars] => Array ( ) )
とあるので、
add_filter(
'snow_monkey_get_template_part_args',
function( $args ) {
if ( 'template-parts/content/entry/content/content' === $args['slug'] && 'post' === $args['name'] ) {
// 読み込むテンプレートのパターンを変更する
$args['slug'] = 'template-parts/single/post';
}
return $args;
}
);
とし、templateを作成する。renameしたtemplateと同じものを、以下のように作る
add_filter(
'snow_monkey_get_template_part_template-parts/single/post',
function( $name, $vars ) {
echo 'contents here';
},
10,
2
);
