wordpressの必要最低限のサンプル
親切なサイトがたくさんありますが、詳細な説明のブログページを読み込んでいく時間がなく、超シンプルなテンプレートのひな形を作りました。各テンプレートファイルの必要最低限の記載です。
とりあえず貼り付けての改造用にどうぞ。
- style.css
必須のテンプレートの1つで、テーマ名の記載が必要です。
/*
Theme Name: Minimal Theme
Theme URI:
Author: Your Name
Author URI:
Description: A minimal WordPress theme
Version: 1.0
License: GNU General Public License v2 or later
*/
/* style.css */
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 0;
}
.container {
width: 80%;
margin: 0 auto;
}
- header.php
/* header.php */
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo('charset'); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php wp_title('|', true, 'right'); ?></title>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<header>
<div class="container">
<h1><a href="<?php echo home_url(); ?>"><?php bloginfo('name'); ?></a></h1>
<nav>
<?php wp_nav_menu(array('theme_location' => 'primary-menu')); ?>
</nav>
</div>
</header>
- index.php
/* index.php */
<?php get_header(); ?>
<main class="container">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</article>
<?php endwhile; endif; ?>
</main>
<?php get_footer(); ?>
- front-page.php
/* front-page.php */
<?php get_header(); ?>
<main class="container">
<section class="hero">
<h2><?php bloginfo('description'); ?></h2>
</section>
<section class="latest-posts">
<h2>最新の記事</h2>
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 3
);
$latest_posts = new WP_Query($args);
if ($latest_posts->have_posts()) : while ($latest_posts->have_posts()) : $latest_posts->the_post();
?>
<article>
<?php if (has_post_thumbnail()) : ?>
<div class="post-thumbnail">
<?php the_post_thumbnail('medium'); ?>
</div>
<?php endif; ?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt(); ?>
</article>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
</section>
</main>
<?php get_footer(); ?>
- page.php
/* page.php */
<?php get_header(); ?>
<main class="container">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="page-content">
<h1><?php the_title(); ?></h1>
<?php if (has_post_thumbnail()) : ?>
<div class="page-thumbnail">
<?php the_post_thumbnail('large'); ?>
</div>
<?php endif; ?>
<div class="page-text">
<?php the_content(); ?>
</div>
</article>
<?php endwhile; endif; ?>
</main>
<?php get_footer(); ?>
- single.php
/* single.php */
<?php get_header(); ?>
<main class="container">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="single-post">
<header class="post-header">
<h1><?php the_title(); ?></h1>
<div class="post-meta">
<time><?php the_time('Y年n月j日'); ?></time>
<span class="post-author"><?php the_author(); ?></span>
<span class="post-categories"><?php the_category(', '); ?></span>
</div>
</header>
<?php if (has_post_thumbnail()) : ?>
<div class="post-thumbnail">
<?php the_post_thumbnail('large'); ?>
</div>
<?php endif; ?>
<div class="post-content">
<?php the_content(); ?>
</div>
<footer class="post-footer">
<?php the_tags('<div class="post-tags">タグ: ', ', ', '</div>'); ?>
<?php comments_template(); ?>
</footer>
</article>
<?php endwhile; endif; ?>
</main>
<?php get_footer(); ?>
- archive.php
/* archive.php */
<?php get_header(); ?>
<main class="container">
<header class="archive-header">
<h1>
<?php
if (is_category()) {
single_cat_title('カテゴリー: ');
} elseif (is_tag()) {
single_tag_title('タグ: ');
} elseif (is_author()) {
echo '投稿者: ' . get_the_author();
} elseif (is_date()) {
if (is_year()) {
echo get_the_date('Y年').'の記事';
} elseif (is_month()) {
echo get_the_date('Y年n月').'の記事';
}
} else {
echo 'アーカイブ';
}
?>
</h1>
</header>
<?php if (have_posts()) : ?>
<div class="archive-posts">
<?php while (have_posts()) : the_post(); ?>
<article class="archive-post">
<?php if (has_post_thumbnail()) : ?>
<div class="post-thumbnail">
<?php the_post_thumbnail('medium'); ?>
</div>
<?php endif; ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="post-meta">
<time><?php the_time('Y年n月j日'); ?></time>
</div>
<?php the_excerpt(); ?>
</article>
<?php endwhile; ?>
<div class="pagination">
<?php echo paginate_links(); ?>
</div>
</div>
<?php else : ?>
<p>記事が見つかりませんでした。</p>
<?php endif; ?>
</main>
<?php get_footer(); ?>
- footer.php
/* footer.php */
<footer>
<div class="container">
<p>© <?php echo date('Y'); ?> <?php bloginfo('name'); ?></p>
</div>
</footer>
<?php wp_footer(); ?>
</body>
</html>
- 404.php
/* 404.php */
<?php get_header(); ?>
<main class="container">
<div class="error-404">
<h1>404 - ページが見つかりません</h1>
<p>お探しのページは存在しないか、移動した可能性があります。</p>
<div class="search-form">
<h2>サイト内検索</h2>
<?php get_search_form(); ?>
</div>
<div class="recent-posts">
<h2>最近の投稿</h2>
<ul>
<?php
wp_get_archives(array(
'type' => 'postbypost',
'limit' => 5
));
?>
</ul>
</div>
</div>
</main>
<?php get_footer(); ?>
- searchform.php
/* searchform.php */
<form role="search" method="get" class="search-form" action="<?php echo home_url('/'); ?>">
<label>
<span class="screen-reader-text">検索:</span>
<input type="search" class="search-field" placeholder="検索..." value="<?php echo get_search_query(); ?>" name="s" />
</label>
<button type="submit" class="search-submit">検索</button>
</form>
- search.php
/* search.php */
<?php get_header(); ?>
<main class="container">
<header class="page-header">
<h1>
<?php
printf(
'検索結果: %s',
'<span>' . get_search_query() . '</span>'
);
?>
</h1>
</header>
<?php if (have_posts()) : ?>
<div class="search-results">
<?php while (have_posts()) : the_post(); ?>
<article class="search-result">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="entry-meta">
<span class="post-type"><?php echo get_post_type(); ?></span>
<time><?php the_time('Y年n月j日'); ?></time>
</div>
<?php the_excerpt(); ?>
</article>
<?php endwhile; ?>
<?php echo paginate_links(); ?>
</div>
<?php else : ?>
<div class="no-results">
<p>検索条件に一致する記事は見つかりませんでした。</p>
<?php get_search_form(); ?>
</div>
<?php endif; ?>
</main>
<?php get_footer(); ?>
- sidebar.php
/* sidebar.php */
<aside class="widget-area">
<?php if (is_active_sidebar('sidebar-1')) : ?>
<?php dynamic_sidebar('sidebar-1'); ?>
<?php else : ?>
<section class="widget">
<h2 class="widget-title">カテゴリー</h2>
<ul>
<?php wp_list_categories('title_li='); ?>
</ul>
</section>
<section class="widget">
<h2 class="widget-title">アーカイブ</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>
</section>
<?php endif; ?>
</aside>
- comments.php
/* comments.php */
<?php
if (post_password_required()) {
return;
}
?>
<div id="comments" class="comments-area">
<?php if (have_comments()) : ?>
<h2 class="comments-title">
<?php
printf(
_n(
'コメント(%1$s)',
'コメント(%1$s)',
get_comments_number()
),
number_format_i18n(get_comments_number())
);
?>
</h2>
<ol class="comment-list">
<?php
wp_list_comments(array(
'style' => 'ol',
'short_ping' => true,
'avatar_size' => 60,
));
?>
</ol>
<?php if (get_comment_pages_count() > 1 && get_option('page_comments')) : ?>
<nav class="comment-navigation">
<div class="nav-previous"><?php previous_comments_link('前のコメント'); ?></div>
<div class="nav-next"><?php next_comments_link('次のコメント'); ?></div>
</nav>
<?php endif; ?>
<?php endif; ?>
<?php if (!comments_open() && get_comments_number() && post_type_supports(get_post_type(), 'comments')) : ?>
<p class="no-comments">コメントは締め切られました。</p>
<?php endif; ?>
<?php comment_form(); ?>
</div>
- template-parts/content.php
/* template-parts/content.php */
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php if (is_singular()) : ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php else : ?>
<h2 class="entry-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h2>
<?php endif; ?>
<?php if ('post' === get_post_type()) : ?>
<div class="entry-meta">
<span class="posted-on">
<?php the_time('Y年n月j日'); ?>
</span>
<span class="byline">
<?php the_author_posts_link(); ?>
</span>
</div>
<?php endif; ?>
</header>
<?php if (has_post_thumbnail() && !is_singular()) : ?>
<div class="post-thumbnail">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('medium'); ?>
</a>
</div>
<?php endif; ?>
<div class="entry-content">
<?php
if (is_singular()) {
the_content();
} else {
the_excerpt();
echo '<a href="' . esc_url(get_permalink()) . '" class="more-link">続きを読む</a>';
}
?>
</div>
</article>
- functions.php
/* functions.php */
<?php
//スイッチオフになっている機能をオンにする
function minimal_theme_setup() {
register_nav_menus(array('primary-menu' => 'Primary Menu')); //管理画面でメニューの作成・編集が可能になる
add_theme_support('automatic-feed-links'); //RSSフィードのリンクを自動的に出力
add_theme_support('post-thumbnails'); //投稿やページでアイキャッチ画像を使用可能に
add_theme_support('title-tag'); //ページタイトルの自動出力をサポート
}
add_action('after_setup_theme', 'minimal_theme_setup');
//jsとCSS読み込み
function my_script_init() {
wp_enqueue_style("font-awesome", "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.2/css/all.min.css", array(), "5.8.2", "all");
wp_enqueue_style("my", get_template_directory_uri() . "/css/style.css", array(), filemtime(get_theme_file_path('css/style.css')), "all");
wp_enqueue_script("my", get_template_directory_uri() . "/js/script.js", array("jquery"), filemtime(get_theme_file_path('js/script.js')), true);
}
add_action("wp_enqueue_scripts", "my_script_init");
- カスタム投稿タイプ(ACFのプラグインを使った場合)
// カスタム投稿タイプを登録
function create_book_post_type() { // 本の投稿タイプを作成する関数を定義
$labels = array( // 管理画面に表示されるラベルを設定
'name' => '本', // 投稿タイプの一般名称
'singular_name' => '本', // 投稿タイプの単数形の名称
'menu_name' => '本', // 管理メニューに表示される名前
'add_new' => '新規追加', // 新規追加ボタンのテキスト
'add_new_item' => '新しい本を追加', // 新規追加画面のタイトル
'edit_item' => '本を編集', // 編集画面のタイトル
'new_item' => '新しい本', // 新規作成時のタイトル
'view_item' => '本を表示', // 表示画面のタイトル
'search_items' => '本を検索', // 検索ボタンのテキスト
'not_found' => '本が見つかりませんでした', // 検索結果が0件の時のメッセージ
'not_found_in_trash'=> 'ゴミ箱に本は見つかりませんでした' // ゴミ箱が空の時のメッセージ
);
$args = array( // カスタム投稿タイプの設定を定義
'labels' => $labels, // 上で設定したラベルを使用
'public' => true, // 投稿タイプを公開する
'has_archive' => true, // アーカイブページを有効にする
'publicly_queryable' => true, // 投稿タイプをクエリで取得可能にする
'show_ui' => true, // 管理画面のUIを表示する
'show_in_menu' => true, // 管理メニューに表示する
'menu_position' => 5, // メニューの表示位置(5は投稿の下)
'menu_icon' => 'dashicons-book-alt', // メニューアイコン
'supports' => array('title', 'editor', 'thumbnail', 'excerpt'), // サポートする機能
'rewrite' => array('slug' => 'books') // URLスラッグを設定
);
register_post_type('book', $args); // カスタム投稿タイプを登録
}
add_action('init', 'create_book_post_type'); // WordPress初期化時に関数を実行
// single-book.php(個別ページのテンプレート)
<?php get_header(); ?> // ヘッダーを読み込む
<div class="container"> // コンテナ要素を開始
<div class="content"> // コンテンツ領域を開始
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> // 投稿がある場合のループを開始
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> // 記事要素を開始
<h1 class="entry-title"><?php the_title(); ?></h1> // 記事タイトルを表示
<?php if (has_post_thumbnail()) : ?> // アイキャッチ画像がある場合
<div class="book-thumbnail"> // サムネイル用のdiv要素
<?php the_post_thumbnail('large'); ?> // アイキャッチ画像を大サイズで表示
</div>
<?php endif; ?>
<div class="entry-content"> // 記事本文用のdiv要素
<?php the_content(); ?> // 記事本文を表示
</div>
</article>
<?php endwhile; endif; ?> // ループを終了
</div>
</div>
<?php get_footer(); ?> // フッターを読み込む
// archive-book.php(一覧ページのテンプレート)
<?php get_header(); ?> // ヘッダーを読み込む
<div class="container"> // コンテナ要素を開始
<h1 class="archive-title">本の一覧</h1> // アーカイブページのタイトル
<div class="book-grid"> // 本のグリッドレイアウト用のdiv要素
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> // 投稿がある場合のループを開始
<article class="book-item"> // 各本のアイテム要素
<a href="<?php the_permalink(); ?>"> // 個別ページへのリンク
<?php if (has_post_thumbnail()) : ?> // アイキャッチ画像がある場合
<?php the_post_thumbnail('medium'); ?> // アイキャッチ画像を中サイズで表示
<?php endif; ?>
<h2><?php the_title(); ?></h2> // 本のタイトルを表示
</a>
<?php the_excerpt(); ?> // 抜粋を表示
</article>
<?php endwhile; ?>
<?php the_posts_pagination(array( // ページネーションを表示
'prev_text' => '前へ', // 前ページへのリンクテキスト
'next_text' => '次へ' // 次ページへのリンクテキスト
)); ?>
<?php else : ?> // 投稿がない場合
<p>本が見つかりませんでした。</p> // 「投稿なし」メッセージを表示
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?> // フッターを読み込む
- タクソノミー
簡単なサンプルです
<?php
// カスタムタクソノミーの登録関数
function register_custom_taxonomy() {
// タクソノミーのラベル設定
$labels = array(
'name' => 'カテゴリー', // 管理画面での一般名称
'singular_name' => 'カテゴリー', // 単数形の名称
'search_items' => 'カテゴリーを検索', // 検索ボタンのラベル
'all_items' => 'すべてのカテゴリー', // 全件表示時のラベル
'parent_item' => '親カテゴリー', // 親カテゴリーのラベル
'parent_item_colon' => '親カテゴリー:', // 親カテゴリー選択時のラベル
'edit_item' => 'カテゴリーを編集', // 編集画面のタイトル
'update_item' => 'カテゴリーを更新', // 更新ボタンのラベル
'add_new_item' => '新しいカテゴリーを追加', // 新規追加画面のタイトル
'new_item_name' => '新しいカテゴリー名', // 新規カテゴリー名入力フィールドのラベル
'menu_name' => 'カテゴリー' // 管理メニューでの表示名
);
// タクソノミーの詳細設定
$args = array(
'labels' => $labels, // 上で設定したラベルを使用
'hierarchical' => true, // 階層構造を持つかどうか(カテゴリー形式)
'public' => true, // 公開するかどうか
'show_ui' => true, // 管理画面に表示するか
'show_admin_column' => true, // 投稿一覧にカラムとして表示するか
'show_in_nav_menus' => true, // ナビゲーションメニューで使用可能にするか
'show_tagcloud' => true, // タグクラウドウィジェットで使用可能にするか
'show_in_rest' => true, // RESTAPIで使用可能にするか(ブロックエディタ対応)
);
// タクソノミーを登録
register_taxonomy('custom_category', array('post'), $args);
}
// init フックでタクソノミーを登録
add_action('init', 'register_custom_taxonomy');
// ACFフィールドグループの登録関数
function register_acf_fields() {
// ACFが有効化されているか確認
if (function_exists('acf_add_local_field_group')) {
// フィールドグループの追加
acf_add_local_field_group(array(
'key' => 'group_custom_category', // グループの一意キー
'title' => 'カテゴリー追加フィールド', // 管理画面での表示名
'fields' => array(
// カテゴリー画像フィールド
array(
'key' => 'field_category_image', // フィールドの一意キー
'label' => 'カテゴリー画像', // フィールドのラベル
'name' => 'category_image', // フィールドの名前(データ保存時のキー)
'type' => 'image', // フィールドタイプ
'return_format' => 'array', // 返り値の形式(array/url/id)
'preview_size' => 'medium', // プレビューサイズ
'library' => 'all', // メディアライブラリの制限
),
// 詳細説明フィールド
array(
'key' => 'field_category_description',
'label' => '詳細説明',
'name' => 'category_description',
'type' => 'wysiwyg', // リッチテキストエディタ
'tabs' => 'all', // タブの表示(text/visual)
'toolbar' => 'full', // ツールバーの種類
'media_upload' => 1, // メディアアップロードの許可
),
// カテゴリーカラーフィールド
array(
'key' => 'field_category_color',
'label' => 'カテゴリーカラー',
'name' => 'category_color',
'type' => 'color_picker', // カラーピッカー
),
),
// フィールドグループの表示条件
'location' => array(
array(
array(
'param' => 'taxonomy', // タクソノミー編集画面で表示
'operator' => '==', // 完全一致
'value' => 'custom_category', // 対象のタクソノミー
),
),
),
));
}
}
// ACF初期化時にフィールドを登録
add_action('acf/init', 'register_acf_fields');
// タクソノミーページのクエリ設定を変更する関数
function modify_taxonomy_query($query) {
// カスタムカテゴリーのタクソノミーページかつメインクエリの場合
if (is_tax('custom_category') && $query->is_main_query()) {
$query->set('posts_per_page', 12); // 1ページあたりの表示件数を12件に設定
$query->set('orderby', 'date'); // 日付順で並び替え
$query->set('order', 'DESC'); // 降順(新しい順)
}
}
// pre_get_posts フックでクエリを変更
add_action('pre_get_posts', 'modify_taxonomy_query');
// タクソノミーのメタデータを取得するヘルパー関数
function get_taxonomy_meta($term_id, $field_name) {
// ACFのget_field関数を使用してメタデータを取得
return get_field($field_name, 'custom_category_' . $term_id);
}
// REST APIにカスタムフィールドを追加する関数
function register_taxonomy_meta_rest_field() {
// custom_categoryタクソノミーにcategory_metaフィールドを追加
register_rest_field('custom_category', 'category_meta', array(
'get_callback' => function($term_arr) {
$term_id = $term_arr['id'];
// 各カスタムフィールドの値を配列で返す
return array(
'image' => get_field('category_image', 'custom_category_' . $term_id),
'description' => get_field('category_description', 'custom_category_' . $term_id),
'color' => get_field('category_color', 'custom_category_' . $term_id),
);
},
'schema' => null, // スキーマ定義(必要に応じて追加可能)
));
}
// REST API初期化時にフィールドを登録
add_action('rest_api_init', 'register_taxonomy_meta_rest_field');
<?php
/**
* Template Name: Taxonomy Archive
* このテンプレートはカスタムタクソノミーのアーカイブページを表示します
*/
// ヘッダーを読み込み
get_header(); ?>
<!-- メインコンテナ -->
<div class="container mx-auto px-4">
<!-- タクソノミーのヘッダー部分 -->
<div class="taxonomy-header my-8">
<h1 class="text-3xl font-bold">
<?php single_term_title(); ?> <!-- 現在のタームのタイトルを表示 -->
</h1>
<?php
// タクソノミーの説明文を取得
$term_description = term_description();
// 説明文がある場合のみ表示
if (!empty($term_description)) : ?>
<div class="taxonomy-description mt-4">
<?php echo $term_description; ?> <!-- タームの説明文を表示 -->
</div>
<?php endif; ?>
</div>
<!-- 投稿のグリッドレイアウト -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
<?php if (have_posts()) : ?> <!-- 投稿があるか確認 -->
<?php while (have_posts()) : the_post(); ?> <!-- 投稿のループ開始 -->
<!-- 個別投稿のカード -->
<article id="post-<?php the_ID(); ?>" <?php post_class('bg-white rounded-lg shadow-md overflow-hidden'); ?>>
<?php if (has_post_thumbnail()) : ?> <!-- アイキャッチ画像があるか確認 -->
<div class="post-thumbnail aspect-video">
<?php
// アイキャッチ画像を表示
the_post_thumbnail('medium_large', array('class' => 'w-full h-full object-cover'));
?>
</div>
<?php endif; ?>
<!-- 投稿のコンテンツ部分 -->
<div class="p-4">
<!-- 投稿タイトル -->
<h2 class="text-xl font-semibold mb-2">
<a href="<?php the_permalink(); ?>" class="hover:text-blue-600">
<?php the_title(); ?> <!-- 投稿タイトルを表示 -->
</a>
</h2>
<!-- 投稿メタ情報 -->
<div class="post-meta text-sm text-gray-600 mb-3">
<span class="posted-on">
<?php echo get_the_date(); ?> <!-- 投稿日を表示 -->
</span>
</div>
<!-- 投稿の抜粋 -->
<div class="post-excerpt text-gray-700">
<?php the_excerpt(); ?> <!-- 投稿の抜粋を表示 -->
</div>
<!-- 続きを読むボタン -->
<div class="mt-4">
<a href="<?php the_permalink(); ?>" class="inline-block bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">
続きを読む
</a>
</div>
</div>
</article>
<?php endwhile; ?>
<?php
// ページネーションの表示
the_posts_pagination(array(
'prev_text' => '前へ', // 前ページへのリンクテキスト
'next_text' => '次へ', // 次ページへのリンクテキスト
'mid_size' => 2, // 現在のページの前後に表示するページ数
'class' => 'mt-8' // ページネーションのクラス
));
?>
<?php else : ?>
<!-- 投稿が見つからない場合のメッセージ -->
<div class="no-posts col-span-full text-center py-8">
<p>投稿が見つかりませんでした。</p>
</div>
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?> <!-- フッターを読み込み -->