LoginSignup
2
0

More than 3 years have passed since last update.

wordpressの固定ページから階層分けして静的ページつくってみた【アップデート対策】

Last updated at Posted at 2019-08-28

@DaichiSaitoさんの記事「テーマ内のファイルを静的サイトのように階層分けする方法(WordPress)」を勝手に補足してみた記事です。:bow_tone1:

そっくり真似します

wp-content / themes / twentynineteen(使用しているテーマ)の直下にテンプレート元を作成。
/ member / member.php

<?php
/*
Template Name: メンバーページ
 */

/ member / page-a / page-a.php

<?php
/*
Template Name: ページA
 */

/ member / page-b / page-b.php

<?php
/*
Template Name: ページB
 */

wordpress管理画面→固定ページ→新規追加→右上のネジマーク「設定」→ページ属性:テンプレート
テンプレートの欄を押すとプルダウンで一覧でてくる。
現設定だと「メンバーページ」しか追加されていない。

class-wp-theme.phpを修正

$files = (array) $this->get_files( 'php', 1, true ); //変更前
$files = (array) $this->get_files( 'php', -1, true ); //変更後

再度、固定ページのテンプレートを見てみると「ページA」と「ページB」が追加されている。

補足

コアファイルをいじっているとWordPressアップデート時に上書きされてしまうので対策。
functions.phpに追記


/**
 * 固定ページのテンプレートを下層ファイルも読み込めるようにする設定の際に、wordpress updateで初期値に更新されてしまうため、
 * 対策として文字列'get_files( 'php', 1, true )'が存在するとき'get_files( 'php', -1, true )'に置換する処理。
 */
function class_wp_theme_convert()
{
    $data = file_get_contents('../wp-includes/class-wp-theme.php');

    if (strpos($data, "get_files( 'php', 1, true )") !== false) {
        $data_convert = str_replace("get_files( 'php', 1, true )", "get_files( 'php', -1, true )", $data);
        file_put_contents('../wp-includes/class-wp-theme.php', $data_convert);
    }
}

add_action('init', 'class_wp_theme_convert');

とりあえず動けーな実装なので、アクションフックinitは毎回処理呼んじゃってイケてないです、イケてるタイミングのフックに修正してください。

参考記事

テーマ内のファイルを静的サイトのように階層分けする方法(WordPress)

2
0
1

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
2
0