Learning site for website creation

17:オリジナルアイキャッチ画像サイズの設定と表示

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

アイキャッチ画像のサイズは管理画面「メディア」から3サイズ設定することができます。

※サイズを変更した場合、既にアップロード済み画像はサイズ変更されないので注意
※アップロード済み画像に変更を適用させる場合は「Regenerate Thumbnails」プラグイン等を使用する

the_post_thumbnail(‘thumbnail’) サムネイル (デフォルト 150px x 150px :最大値)
the_post_thumbnail(‘medium’) 中サイズ (デフォルト 300px x 300px :最大値)
the_post_thumbnail(‘large’) 大サイズ (デフォルト 1024px x 1024px :最大値)
the_post_thumbnail(‘full’) フルサイズ (アップロードした画像の元サイズ)

オリジナルサイズのアイキャッチ画像を設定

3サイズ以上の画像サイズをテーマ内で使いたい場合は以下の関数を「functions.php」に記述します。

functions.php

//オリジナルアイキャッチ画像の設定
add_image_size('thumb_100_100', 100, 100);
add_image_size('thumb_100_100_cut', 100, 100, true);
第1引数(string)(必須) 初期値:なし 新しい画像サイズの名前
第2引数(int) 初期値:0 投稿サムネイルの幅をピクセル数で指定
第3引数(int) 初期値:0 投稿サムネイルの高さをピクセル数で指定
第4引数(boolean) 初期値:false true:切り抜きする, false:切り抜きしない

※「add_image_size()」は「add_theme_support(‘post-thumbnails’);」を実行していないと機能しません

オリジナルサイズのアイキャッチ画像を表示する

index.php

ハイライト部分 the_post_thumbnail関数の引数をオリジナルアイキャッチ画像名に変更

<?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 // アイキャッチ画像 ?>
  <div class="thumbnail">
<?php
if (has_post_thumbnail()) {
  the_post_thumbnail('thumb_100_100');
}else{
  echo '<img src="'.get_template_directory_uri().'/img/no-image.png" alt="'.get_the_title().'">'.PHP_EOL;
}
?>
  </div>

<?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();

※サイズを変更した場合、既にアップロード済み画像はサイズ変更されないので注意
※アップロード済み画像に変更を適用させる場合は「Regenerate Thumbnails」プラグイン等を使用する

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

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

WordPress関数

add_image_size()

 

欠席者対応:wireframe05

同じタグのコンテンツ