LoginSignup
3
0

More than 1 year has passed since last update.

WordPressの固定ページのテンプレートで迷子になった

Posted at

WordPressを触れ始めて、早9ヶ月。
自分でサイトを作成しよう!と思い立ったところで躓いた。

カスタムテンプレートが表示されない

以下テンプレート用のフォルダ構成

/template
├ /article
|├ page-articles.php(テストページ)
|└ /image_gallery
|  └ page-gallery.php(ギャラリーページ)
├ page-lib.php(ライブラリページ)
└ page-top.php(トップページ)

フォルダ構成の通りにカスタムテンプレートできた!
後はこいつを固定ページで…
screenshot_2021_12_26_template_before.png

テストページとギャラリーページのテンプレートがいない?
どうしたものかと、Google先生を頼る。
そして見つけたサイトを見てみると
/wp-includes/class-wp-theme.php でテンプレートの深さはハードコーディングされているとのこと。

該当コード

/wp-includes/class-wp-theme.php $\tiny{(1190行目あたり)}$

public function get_post_templates() {
        // If you screw up your current theme and we invalidate your parent, most things still work. Let it slide.
        if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) ) {
            return array();
        }

        $post_templates = $this->cache_get( 'post_templates' );

        if ( ! is_array( $post_templates ) ) {
            $post_templates = array();

            $files = (array) $this->get_files( 'php', 1, true );

            foreach ( $files as $file => $full_path ) {
                if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) ) {
                    continue;
                }

                $types = array( 'page' );
                if ( preg_match( '|Template Post Type:(.*)$|mi', file_get_contents( $full_path ), $type ) ) {
                    $types = explode( ',', _cleanup_header_comment( $type[1] ) );
                }

\$files = (array) $this->get_files( 'php', 1, true );
*赤文字がテンプレートを表示する階層を示しているようです。

数字を変えてみるとどうなるか実験してみた

実験開始

第1段階 : 階層の深さを2にしてみる

スクリーンショット 2021-12-26 1.20.07.png

迷子だったテストページのテンプレートが見つかりました。
しかし、まだギャラリーページが見つからない。でも数字を変えることで表示されることがわかった。

第2段階 : 全件表示するために深さを-1に変更してみた

スクリーンショット 2021-12-26 1.20.24.png

無事、テンプレートが全て表示された。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0