Title: 投稿記事の１枚目を表示でnoimageの画像が出ない
Last modified: 2021年8月24日

---

# 投稿記事の１枚目を表示でnoimageの画像が出ない

 *  解決済 [chatm](https://ja.wordpress.org/support/users/chatm/)
 * (@chatm)
 * [5年、 1ヶ月前](https://ja.wordpress.org/support/topic/%e6%8a%95%e7%a8%bf%e8%a8%98%e4%ba%8b%e3%81%ae%ef%bc%91%e6%9e%9a%e7%9b%ae%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%a7noimage%e3%81%ae%e7%94%bb%e5%83%8f%e3%81%8c%e5%87%ba%e3%81%aa%e3%81%84/)
 * 投稿記事の１枚目の画像を取得してサムネイル画像として、カテゴリーページに一覧にし
   ていますが、function.phpに掲載した、画像がない場合に表示するnoimage画像が表示
   されません。
    （ソースでは「$first_img = $matches [1][0];」のところがエラーに
   なっているらしいのですが） 絶対座標にしても表示されません（画像だけのURLを打ち込む
   と表示されますが、組み込むと表示されません）。原因が分からず困っています。教え
   て頂けると助かります。
 *     ```
       // 投稿画像の１枚目の画像をサムネイルとして出力する。ない場合はnoimage.pngを出力
   
       function catch_that_image() {
           global $post, $posts;
           $first_img = '';
           ob_start();
           ob_end_clean();
           $output = preg_match( '/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]+\>/i', $post->post_content, $matches );
       	$first_img = $matches [1][0];
   
           if(empty($first_img)){ //Defines a default image
               $first_img = esc_url(get_template_directory_uri()) . "/images/noimage.png";
           }
       return $first_img;
       }
       ```
   

2件の返信を表示中 - 1 - 2件目 (全2件中)

 *  [shokun0803](https://ja.wordpress.org/support/users/shokun0803/)
 * (@shokun0803)
 * [5年、 1ヶ月前](https://ja.wordpress.org/support/topic/%e6%8a%95%e7%a8%bf%e8%a8%98%e4%ba%8b%e3%81%ae%ef%bc%91%e6%9e%9a%e7%9b%ae%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%a7noimage%e3%81%ae%e7%94%bb%e5%83%8f%e3%81%8c%e5%87%ba%e3%81%aa%e3%81%84/#post-11674083)
 * chatmさん、こんにちは。
 * > 投稿記事の１枚目の画像を取得してサムネイル画像として、カテゴリーページに一覧
   > にしていますが、function.phpに掲載した、画像がない場合に表示するnoimage画像
   > が表示されません。
 * んー、間違っていたらごめんなさい。そもそも preg_match で img タグを探している
   ようですけども、`$first_img = $matches [1][0];`ここで画像が存在していなければ
   $matches [1][0] は配列として空で存在しなくないですか。
    先に $matches [1][0] 
   が存在するかどうかを調べないとここでエラーがでる気がするのですが…エラーコード
   は存在しないとかになっていませんかね…
 * ご参考になれば。
 *  トピック投稿者 [chatm](https://ja.wordpress.org/support/users/chatm/)
 * (@chatm)
 * [5年、 1ヶ月前](https://ja.wordpress.org/support/topic/%e6%8a%95%e7%a8%bf%e8%a8%98%e4%ba%8b%e3%81%ae%ef%bc%91%e6%9e%9a%e7%9b%ae%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%a7noimage%e3%81%ae%e7%94%bb%e5%83%8f%e3%81%8c%e5%87%ba%e3%81%aa%e3%81%84/#post-11674087)
 * できました！本当にためになります。ありがとうございました！
    出来上がったコード
   です。
 *     ```
       // サムネイル画像 / 投稿画像の１枚目の画像をサムネイルとして出力する。ない場合はnoimage.pngを出力
   
       function catch_that_image() {
           global $post, $posts;
           $first_img = '';
           ob_start();
           ob_end_clean();
           if (preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches)){
               $first_img = $matches [1] [0];
           }else{
               $first_img = get_stylesheet_directory_uri().'/images/noimage.png';
           }
       return $first_img;
       }
       ```
   

2件の返信を表示中 - 1 - 2件目 (全2件中)

 トピック「投稿記事の１枚目を表示でnoimageの画像が出ない」には新たに返信すること
はできません。

## タグ

 * [サムネイル画像](https://ja.wordpress.org/support/topic-tag/%e3%82%b5%e3%83%a0%e3%83%8d%e3%82%a4%e3%83%ab%e7%94%bb%e5%83%8f/)

 * フォーラム: [使い方全般](https://ja.wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2件の返信
 * 2人の参加者
 * 最後の返信者: [chatm](https://ja.wordpress.org/support/users/chatm/)
 * 最後の活動: [5年、 1ヶ月前](https://ja.wordpress.org/support/topic/%e6%8a%95%e7%a8%bf%e8%a8%98%e4%ba%8b%e3%81%ae%ef%bc%91%e6%9e%9a%e7%9b%ae%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%a7noimage%e3%81%ae%e7%94%bb%e5%83%8f%e3%81%8c%e5%87%ba%e3%81%aa%e3%81%84/#post-11674087)
 * 状態: 解決済

## トピック

### 人気のトピック

### 返信がないトピック

### サポート関連ではないトピック

### 解決済みのトピック

### 未解決のトピック

### すべてのトピック
