0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[WordPress] テンプレートタグ基本

Last updated at Posted at 2025-09-15

必須のテンプレートタグ(2つ)

  • wp_head()
  • wp_footer()

これらをおくことで、サイトにメニューバーが表示される。
メニューバーが不要でも、WPの動作には必須なので、必ずおくこと。

    <?php wp_head(); ?>
  </head>
  <?php wp_footer(); ?>
</body>

必須でないテンプレートタグ

have_posts()

投稿があるか(投稿一覧に結果があるか)
TrueまたはFalseがかえる

<?php the_date(); ?>

the_date() は 「日付が変わったときだけ出力する」 という関数。
つまり、ループの中で複数の投稿が 同じ日付 の場合、最初の1件だけ 日付を表示し、以降はスキップ。

  • ダッシュボード → 設定 → 一般 → 日付形式 から表示方法を変更できる
<?php the_time(); ?>

全てに
8:07 pm
と出力される

<?php the_time('Y/m/d'); ?>

全てに
2025/09/14
と出力される

<?php the_time(get_option('date_format')); ?>

全てに
2025/09/14
と出力される
※ダッシュボードで指定した日付形式で表示

<?php echo get_the_date(); ?>

全てに
2025/09/14
と出力される

🔹 覚え方

  • the_〇〇 → そのまま 出力

  • get_the_〇〇 → 値を 取得(返す)のでechoがいる

WordPress のテンプレートタグはこの規則が多い。

代替構文 (Alternative syntax)

  • {}の代わりに: と endif;などと書くPHP 標準の文法
  • テンプレートファイルや HTML と混在する場合に便利
  • if, for, foreach, while, switch すべてで使える

1️⃣ if 文

<?php if ($condition) : ?>
    条件が真のときの処理
<?php elseif ($other_condition) : ?>
    別の条件が真のときの処理
<?php else : ?>
    条件が偽のときの処理
<?php endif; ?>

2️⃣ foreach 文

<?php foreach ($items as $item) : ?>
    <p><?php echo $item; ?></p>
<?php endforeach; ?>

3️⃣ for 文

<?php for ($i = 0; $i < 5; $i++) : ?>
    <p>番号: <?php echo $i; ?></p>
<?php endfor; ?>

4️⃣ while 文

<?php while ($count < 5) : ?>
    <p>カウント: <?php echo $count; ?></p>
    <?php $count++; ?>
<?php endwhile; ?>

5️⃣ switch 文

<?php switch ($var) : ?>
    <?php case 1: ?>
        <p>1です</p>
        <?php break; ?>
    <?php case 2: ?>
        <p>2です</p>
        <?php break; ?>
    <?php default: ?>
        <p>その他です</p>
<?php endswitch; ?>

例文1

        <?php if (have_posts()) : ?>
          <div class="post-preview">
            <a href="post.html">
              <h2 class="post-title">
                <?php the_post(); ?>
                <?php the_title(); ?>
              </h2>
            </a>
          </div>
        <?php else: ?>
          <p>記事が見つかりませんでした。</p>
        <?php endif; ?>

例文2

        <?php $lists = have_posts(); ?>
        <?php if ($lists) : ?>
          <div class="post-preview">
            <a href="post.html">
              <h2 class="post-title">
                <?php the_post(); ?>
                <?php the_title(); ?>
              </h2>
            </a>
          </div>
        <?php else: ?>
          <p>記事が見つかりませんでした。</p>
        <?php endif; ?>

例文3
have_posts() は the_post() と一緒に使わないと無限ループになるので気を付ける

<?php while (have_posts()): ?>
    <?php the_post(); ?>
    <?php the_title(); ?>
<?php endwhile; ?>

例文3
上記と同じ意味

<?php while (have_posts()): the_post(); ?>
    <?php the_title(); ?>
<?php endwhile; ?>

まとめ

WordPress では

  • 必須のテンプレートタグ wp_head() と wp_footer()
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?