WordPress では該当するページがない場合、優先的に index.php が表示されるようになっているので
<?php get_header(); ?> <div id="content"> <h2>指定されたページは存在しませんでした。</h2> <p>このページの URL : <span class="error_msg"> http://<?php echo esc_html($_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']); ?> </span></p> <p>現在表示する記事がありません。</p> <p><a href="<?php echo home_url(); ?>">トップページへ戻る</a></p> </div> <?php get_footer(); ?>
エラーメッセージを出し、表示中のページの URL を「echo esc_html($_SERVER[‘SERVER_NAME’].$_SERVER[‘REQUEST_URI’]);」で出力。最下部にトップページへ戻るリンクを出力
エラーが出ても 404.php でページが生成されない場合があるので、.htaccess に「ErrorDocument 404 /index.php?error=404」という設定を追加する。
※WordPress をサブディレクトリにインストールしている場合は、「ErrorDocument 404 /サブディレクトリ名/index.php?error=404」という形で指定する必要がある。
.htaccessファイル
# BEGIN WordPress ・・・省略・・・ # END WordPress ErrorDocument 404 /index.php?error=404