[2016.03] PHOTO BIYORI Update

WordPress 4.4以降での新規投稿エラーへの対応です。
このアップデートで、従来の抜粋フィールドにサムネイルコードを挿入する方法を廃止し、自動でアイキャッチ画像(Featured Image)を作成、その画像をサムネイルに使用する方法に変更します。

変更するのはPhoto Biyoriテーマ内の functions.php ファイルです。
以下の変更前(PB3-LT 3.0.3) と変更後のファイルをダウンロードして作業の参考にしてください。

PB3-LT-Update.zip

今お使いのfunctions.phpファイルをご自分でカスタマイズされず、そのまま使っている場合は、functions-updated.phpのファイル名をfunctions.phpに変更して、そのまま使用していただいても大丈夫です。
いずれの場合も、必ず現在お使いのファイルのバックアップを取っておいてください。


1. 178行目のコードをコメントアウト:
add_filter('excerpt_save_pre', 'pb_insert_excerpt');
     ↓
// add_filter('excerpt_save_pre', 'pb_insert_excerpt');

この状態で、新規投稿の際のエラーメッセージが消えるはずですので、エラーが表示されないかどうか確認してください。


2. 以下のコードを最初の < ?php の後に追加:

add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 60, 60, true );


数字の60はサムネイル画像のサイズです。カスタマイズされている場合はご自分のサムネイル画像サイズに合わせてください。


3. 以下のコードを最後の ?> の前に追加:

function auto_featured_image() {
    global $post;
    if (!has_post_thumbnail($post->ID)) {
        $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
         
      if ($attached_image) {
              foreach ($attached_image as $attachment_id => $attachment) {
                   set_post_thumbnail($post->ID, $attachment_id);
              }
         }
    }
}
// Use it temporary to generate all featured images
add_action('the_post', 'auto_featured_image');
// Used for new posts
add_action('save_post', 'auto_featured_image');
add_action('draft_to_publish', 'auto_featured_image');
add_action('new_to_publish', 'auto_featured_image');
add_action('pending_to_publish', 'auto_featured_image');
add_action('future_to_publish', 'auto_featured_image');


4. 以下のコードを変更
17行目 function pb_recent_thumbnails ($limit) {
$post_excerpt = ($result->post_excerpt);
     ↓
$post_excerpt = get_the_post_thumbnail($result->ID,'thumbnail');

33行目 function pb_before_after_thumbnails ($limit) {
$post_excerpt = ($before_result->post_excerpt);
     ↓
$post_excerpt = get_the_post_thumbnail($before_result->ID,'thumbnail');

$post_excerpt = ($cur_result->post_excerpt);
     ↓
$post_excerpt = get_the_post_thumbnail($cur_result->ID,'thumbnail');

$post_excerpt = ($after_result->post_excerpt);

     ↓
$post_excerpt = get_the_post_thumbnail($after_result->ID,'thumbnail');

同じようですが少しずつ違うコードの変更が4カ所あります。


5. archive.phpとalbum.phpの変更
< ?php the_excerpt(); ?>
     ↓
< ?php the_post_thumbnail('thumbnail'); ?>

最初の<と?phpの間のスペースはなくしてください。 それぞれ1カ所です。


6. サムネイルの自動作成
ここまで作業が完了したら、ご自分のサイトのトップページにアクセスしてください。
最新のサムネイルがひとつだけ表示される状態になっているはずです。この状態のまま、アルバムページ(すべての投稿の一覧ページ)を開いてください。ここでサムネイルが再度作られますので、"older"リンクで最後(一番古いページ)まで開いて、すべてのサムネイルが作成されていくのを確認してください。
こののち、トップページにアクセス、サムネイルが表示されていることを確認してください。


7. 新規投稿テスト
投稿下書き保存で、アイキャッチ画像(Featured Image)にサムネイル画像が自動で作成されることを確認してください。


8. 過去のサムネイル作成機能の削除
6.の作業ですべてのサムネイルが作成されたことを確認したのち、functions.phpファイルの最後に追加したコードを変更(コメントアウト):
add_action('the_post', 'auto_featured_image');
     ↓
// add_action('the_post', 'auto_featured_image');