Learning site for website creation

15:タグを表示する

公開日:2018年11月04日 更新日:2018年11月06日

投稿はタグを複数もつことができます。カテゴリーと違いタグをもっていない投稿もあります。タグを表示させましょう。

タグ表示用関数はページ内容表示用ループ内で使用します。
タグ表示は「投稿」に設定できる機能なので「投稿一覧」と「投稿詳細」で使用します。

リンクつきタグ表示簡易版

WordPress関数にリンクつきタグ表示関数が用意されています。

<?php if (has_tag()) : ?>
<h3 class="tag-title">タグ:</h3>
<?php the_tags('<ul class="tags"><li>', '</li><li>', '</li></ul>'); ?>
<?php endif; ?>

HTML出力結果

<h3 class="tag-title">タグ:</h3>
<ul class="tags">
  <li>
    <a href="http://localhost/lesson1/tag/タグスラッグ/" rel="tag">タグ名</a>
  </li>
</ul>

リンクつきタグ表示詳細版

HTMLの構成を変えたりする場合は詳細版を使用します。

<?php // タグ文字列 ?>
<?php if (has_tag()) : ?>
  <h3 class="tag-title">タグ:</h3>
  <ul class="tags-list">
<?php
$tagList = get_the_tags();
for ($i=0; $i < count($tagList); $i++) :
  $tagID = $tagList[$i]->term_id;
  $tagName = $tagList[$i]->name;
?>
    <li class="tag-id<?php echo $tagID; ?>">
      <a href="<?php echo get_tag_link($tagID); ?>">
        <?php echo $tagName; ?>
      </a>
    </li>
<?php endfor; ?>
  </ul>
<?php endif; ?>

HTML出力結果

  <h3 class="tag-title">タグ:</h3>
  <ul class="tags-list">
    <li class="tag-idタグID番号">
      <a href="http://localhost/lesson1/tag/タグスラッグ/">
        タグ名
      </a>
    </li>
  </ul>>

リンクなしタグ表示

投稿一覧ページではリンクがかぶらないようリンクなしタグを使用します。

<?php // タグ文字列 ?>
<?php if (has_tag()) : ?>
  <h3 class="tag-title">タグ:</h3>
  <ul class="tags-list">
<?php
$tagList = get_the_tags();
for ($i=0; $i < count($tagList); $i++) :
  $tagID = $tagList[$i]->term_id;
  $tagName = $tagList[$i]->name;
?>
    <li class="tag-id<?php echo $tagID; ?>">
      <?php echo $tagName; ?>
    </li>
<?php endfor; ?>
  </ul>
<?php endif; ?>

HTML出力結果

  <h3 class="tag-title">タグ:</h3>
  <ul class="tags-list">
    <li class="tag-idタグID番号">
      タグ名
    </li>
  </ul>

テンプレート記述位置

index.php

ハイライト部分を追記

<?php get_header(); ?>

  <div class="content">
    <div class="main">

      <div class="main-loop">
        <div class="inner">
<?php /***** メインループ開始 *****/ ?>
<?php if (have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<?php $id = get_the_ID(); ?>
<?php // リンク ?>
<a class="post-link <?php echo 'post-id'.$id; ?>" href="<?php the_permalink(); ?>">

<?php // タイトル ?>
  <h2 class="title"><?php the_title(); ?></h2>

<?php // カテゴリ文字列 ?>
  <h3 class="category-title">カテゴリ:</h3>
  <ul class="category-list">
<?php
$category_list = get_the_category();
for ($i=0; $i < count($category_list); $i++) :
  $catID = $category_list[$i]->term_id;
  $catName = $category_list[$i]->name;
  $catDescription = $category_list[$i]->description;
  $catColor = '';
  if($catDescription) {
    $catColor = ' style="background-color:'.$catDescription.';"';
  }
?>
    <li class="cat-id<?php echo $catID; ?>"<?php echo $catColor; ?>>
      <?php echo $catName; ?>
    </li>
<?php endfor; ?>
  </ul>

<?php // タグ文字列 ?>
<?php if (has_tag()) : ?>
  <h3 class="tag-title">タグ:</h3>
  <ul class="tags-list">
<?php
$tagList = get_the_tags();
for ($i=0; $i < count($tagList); $i++) :
  $tagID = $tagList[$i]->term_id;
  $tagName = $tagList[$i]->name;
?>
    <li class="tag-id<?php echo $tagID; ?>">
      <?php echo $tagName; ?>
    </li>
<?php endfor; ?>
  </ul>
<?php endif; ?>

<?php // 抜粋文 ?>
  <div class="excerpt"><?php the_excerpt(); ?></div>

<?php // 公開日時 ?>
  <div class="release-date">公開日時:<?php the_time('Y年m月d日'); ?></div>

<?php // 最終更新日時 ?>
  <div class="modified-date">最終更新日時:<?php the_modified_date('Y年m月d日'); ?></div>
</a>

<?php endwhile; ?>
<?php else: ?>
<div class="no-post">
  <div class="inner">
    <p>記事が存在しません</p>
  </div><!-- /.inner -->
</div><!-- /.no-post -->
<?php endif; ?>
<?php /***** メインループ終了 *****/ ?>

        </div><!-- /.inner -->
      </div><!-- /.main-loop -->
    </div><!-- /.main -->
    <?php get_sidebar(); ?>
  </div><!-- /.content -->


<?php
get_footer();

調べてみよう

タグ管理オブジェクトの内容を表示してみよう。

このページで出てくる関数

WordPress関数は「公式サイトのドキュメント」で確認しましょう。

WordPress関数

the_tags()

get_the_tags()

get_tag_link()

 

欠席者対応:wireframe05

同じタグのコンテンツ