Open4

wordpress

engineer rebornengineer reborn

docker-compose

https://docs.docker.jp/compose/wordpress.html

  • volumeする場所
    • themeのボリューム
    • pluginsのボリューム
version: '3.8'

services:
  wordpress:
    image: wordpress:php8.2-apache
    container_name: wp
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
    volumes:
      # WordPressのテーマフォルダをマウント
      - ./wordpress/themes:/var/www/html/wp-content/themes
      # プラグイン永続化
      - ./wordpress/plugins:/var/www/html/wp-content/plugins
      # 永続化したい場合は以下も
      # - wp_data:/var/www/html

  db:
    image: mysql:8.0
    container_name: wp_db
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - db_data:/var/lib/mysql

volumes:
  db_data:

テーマを自分で作成

project-root/
├── docker-compose.yml
├── wordpress/            # WordPress 関連データ
│   └── themes/           # 独自テーマを配置
│       └── mytheme/      # あなたの独自テーマ
│           ├── style.css
│           ├── functions.php
│           └── index.php
└── db_data/              # MySQLのデータ保存用(volumeとして利用)
engineer rebornengineer reborn

テーマ追加

  • ポイント
    • thmes配下に独自のディレクトリの作成と必要ファイルを入れる
      • ディレクトリ単位で識別される

plugin

AFC

  • 投稿のルールに紐づいて表示される
engineer rebornengineer reborn

基本構文

  • have_postsで投稿があるか?
  • the_postで グローバル変数$postにデータを格納
    • the_IDや the_title, the_contentが使えるようになる
  • ACF
    • get_fieldでフィールドの
<?php
/**
 * メインテンプレート (index.php)
 * 投稿の一覧表示と基本的な WordPress API の使い方サンプル
 */

get_header(); // ヘッダー呼び出し

// 投稿データを取得 (WordPress Loop)
if ( have_posts() ) :
    while ( have_posts() ) :
        the_post(); ?>
        
        <article id="post-<?php the_ID(); ?>">
            <h2><?php the_title(); ?></h2>

            <div class="entry-content">
                <?php the_content(); ?>
            </div>

            <div class="meta">
                <p>公開日: <?php the_time('Y-m-d'); ?></p>
            </div>

            <?php
            // Advanced Custom Fields (ACF) を使ってカスタムフィールドを取得
            if ( function_exists('get_field') ) {
                $custom_value = get_field('test1'); // ACFのフィールド名を指定
                if ( $custom_value ) {
                    echo '<p>ACFカスタムフィールド: ' . esc_html($custom_value) . '</p>';
                }
            }
            ?>
        </article>

    <?php
    endwhile;
else :
    echo '<p>記事がありません。</p>';
endif;

get_footer(); // フッター呼び出し