• 解決済 asx@

    (@asx-1)


    singel.phpで「Notice: Undefined offset: 0 in」というエラーが出てしまいなおせません。

    配列に値がセットされていないというエラーだということはわかったのですが、具体的にどう記述を修正したらよいのかわかりません。

    <?php
    function catch_that_image() {
    global $post, $posts;
    $first_img = ”;
    ob_start();
    ob_end_clean();
    $output = preg_match_all(‘/<img.+src=[\'”]([^\'”]+)[\'”].*>/i’, $post->post_content, $matches);
    $first_img = $matches [1] [0];

    if(empty($first_img)){ //Defines a default image
    $first_img = “http://xn--88jzc9b4e3iye.com/wp-content/uploads/2013/04/ep0.jpg&#8221;;
    }
    return $first_img;
    }
    ?>

    こちらの記述の$first_img = $matches [1] [0];の配列[0]のエラーだと思うのですが、記述をそう直したら良いのかわかりません。

    ご教授よろしくお願いいたします。

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • kz

    (@kz)

    catch_that_image() を以下の jim912 さんの返信のように変更されると良い気がします。
    [解決済み] 記事中一番最初の画像を自動表示するが画像なしの時はHTML非表示にしたい

    トピック投稿者 asx@

    (@asx-1)

    kzさn
    ありがとうございます。

    function catch_that_image() {
    global $post;

    if ( preg_match_all( ‘/<img.+src=[\'”]([^\'”]+)[\'”].*>/i’, $post->post_content, $matches) ) {
    $first_img = $matches[1][0];
    } else {
    $first_img = false;
    }
    return $first_img;
    }

    こちらの記述で修正できました!
    ありがとうございます。

    gatespace

    (@gatespace)

    ※解決したなら右上のプルダウンを「解決済み」にしておいてください。
    (最初にスレッドを開始した人しかできません)

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • トピック「single.phpにエラーがでてしまい対処方法がわかりません。」には新たに返信することはできません。