2016年07月21日

WordPressで、アイキャッチ画像の設定が出来ないのですよ……


他の方がカスタマイズした某テーマの話ですが……

『カスタマイズしたらアイキャッチ画像が設定出来ないですよ?』とご報告を受けました。

わたくし、確かにお手伝いはさせていただいたんですが、
わたくしがお手伝いした部分って、カスタム投稿タイプの部分なんですよ(;・∀・)
アイキャッチ画像はもちろん、カスタム投稿なんかにも関わった覚えがなく……

ともあれ困ったご様子で、
『あ、そうだ……ここはやっていただいてないんですよね……(´・ω・`)』
と相当に落ち込まれていたので、とりあえずお手伝いしてみることに。

最初は症状を確認してみました。まずは通常の投稿の方。
01
なるほど。アイキャッチ画像のチェックが出来ませんね……(;・∀・)

次は、追加されていたカスタム投稿の方。
02
うん。こっちは問題なさそうですな(`・ω・´)

次は利用しているテーマのfunctions.phpの中を見てみることに。
まぁ最初に疑う場所といえばココですよねぇ?

そのfunctions.phpに追加されていたコードを再現したものがこちら。

    add_action( 'init', 'register_cpt_test_custom' );

    function register_cpt_test_custom() {

        $labels = array(
            'name' => _x( 'テスト項目一覧', 'test_custom' ),
            'singular_name' => _x( 'テスト項目一覧', 'test_custom' ),
            'add_new' => _x( 'テスト項目追加', 'test_custom' ),
            'add_new_item' => _x( 'Add New テスト項目', 'test_custom' ),
            'edit_item' => _x( 'テスト項目を編集', 'test_custom' ),
            'new_item' => _x( '新しいテスト', 'test_custom' ),
            'view_item' => _x( 'テスト項目の詳細を見る', 'test_custom' ),
            'search_items' => _x( 'テスト項目検索', 'test_custom' ),
            'not_found' => _x( 'テスト項目が見つかりません', 'test_custom' ),
            'not_found_in_trash' => _x( 'ゴミ箱にテスト項目はありません', 'test_custom' ),
            'parent_item_colon' => _x( '新テスト項目:', 'test_custom' ),
            'menu_name' => _x( 'テスト一覧', 'test_custom' ),
        );

        $args = array(
            'labels' => $labels,
            'hierarchical' => false,
            'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields' ),
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'show_in_nav_menus' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'has_archive' => true,
            'query_var' => true,
            'can_export' => true,
            'rewrite' => true,
            'capability_type' => 'post'
        );

add_theme_support( 'post-thumbnails', array( 'test_custom' ) );
set_post_thumbnail_size( 150, 150, true );


function add_menu_icons_styles(){
     echo '<style>
          #adminmenu #menu-posts-team div.wp-menu-image:before {
               content: "\f154";
          }
     </style>';
}
add_action( 'admin_head', 'add_menu_icons_styles' );


        register_post_type( 'test_custom', $args );
    }

まぁこんな感じ。なんか変(;・∀・)
とりあえず手直ししてみたのがこちら。

    add_action( 'init', 'register_cpt_test_custom' );

    function register_cpt_test_custom() {

        $labels = array(
            'name' => _x( 'テスト項目一覧', 'test_custom' ),
            'singular_name' => _x( 'テスト項目一覧', 'test_custom' ),
            'add_new' => _x( 'テスト項目追加', 'test_custom' ),
            'add_new_item' => _x( 'Add New テスト項目', 'test_custom' ),
            'edit_item' => _x( 'テスト項目を編集', 'test_custom' ),
            'new_item' => _x( '新しいテスト', 'test_custom' ),
            'view_item' => _x( 'テスト項目の詳細を見る', 'test_custom' ),
            'search_items' => _x( 'テスト項目検索', 'test_custom' ),
            'not_found' => _x( 'テスト項目が見つかりません', 'test_custom' ),
            'not_found_in_trash' => _x( 'ゴミ箱にテスト項目はありません', 'test_custom' ),
            'parent_item_colon' => _x( '新テスト項目:', 'test_custom' ),
            'menu_name' => _x( 'テスト一覧', 'test_custom' ),
        );

        $args = array(
            'labels' => $labels,
            'hierarchical' => false,
            'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields' ),
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'show_in_nav_menus' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'has_archive' => true,
            'query_var' => true,
            'can_export' => true,
            'rewrite' => true,
            'capability_type' => 'post'
        );

        register_post_type( 'test_custom', $args );
    }

    add_theme_support( 'post-thumbnails', array( 'test_custom' ) );
    set_post_thumbnail_size( 150, 150, true );


    function add_menu_icons_styles(){
         echo '<style>
              #adminmenu #menu-posts-team div.wp-menu-image:before {
                   content: "\f154";
              }
         </style>';
    }
    add_action( 'admin_head', 'add_menu_icons_styles' );

とりあえずadd_theme_supportの位置を修正してみた感じですかねぇ。
ですが当然というか何というか……効果なし。
add_theme_supportとset_post_thumbnail_sizeの位置は今回の不具合には関係ないのかな?
念のため、追加部分の一番ケツに移動させても意味はなし(´・ω・`)

ならばと追加部分の先頭に、以下のadd_theme_supportを追加。

add_theme_support('post-thumbnails',array( 'post' ));

まぁ要は通常の投稿も『アイキャッチ画像使うよ!』と言ってみたのですよ。
ところがこれでも意味なし。なーんーでー(´・ω・`)

んで、次にやったのが、まず先頭のadd_theme_supportを削除。
ファイルの一番ケツのadd_theme_supportを、
少々いじってみることにしてみた。修正してみたのがこちら。

    add_theme_support( 'post-thumbnails', array( 'test_custom','post' ) );

要はあれですわ。2回『アイキャッチ画像使うよ!』と宣言してたのを、
一回にまとめてみた感じって言えばいいかなぁ?
その結果……

03

おっけー(・∀・) とりあえずこれでアイキャッチ画像が使えるようになりましたな(・∀・)

さて、どうも原因はadd_theme_supportでの投稿タイプの指定が漏れていたこと?
ちょっと他のテーマで実験をしてみましたわ。
アイキャッチ画像が問題なく使えてるテーマにカスタム投稿タイプ『test』を作成。
作成したら、以下のコードをファイルのケツに追加。

add_theme_support('post-thumbnails', array( 'test_custom' ))

その結果……

04

oh……(´・ω・`) アイキャッチ画像消えてますな……(´・ω・`)

どうもadd_theme_supportで投稿名を指定すると、
その投稿タイプだけがアイキャッチ画像有効になるっぽい?
複数回投稿タイプ名を指定しても、追加されるというよりは上書きっぽい?

ここで自分の話になりますが、
当管理人は、ここまでadd_theme_supportで苦しんだ経験てないんですよ。
カスタム投稿タイプの作成の際にも、supportsの項目に’thumbnail’を指定してれば、
何の苦労もせずアイキャッチ画像の指定が可能でしたから(;・∀・)

で、『んじゃなんで今まで苦労せずアイキャッチ画像が指定できてたのか?』と思い、
自分が作成したテーマのfunctions.phpを確認してみたところ、
ファイルの先頭で以下のコードを入れてました。

    add_theme_support( 'post-thumbnails');

要は先頭で投稿名を指定しないで『アイキャッチ画像使いまっせ!!』と宣言してたんすよ。

というわけで、それを踏まえた上で、最終的にこんなコードにしてみました。

    //アイキャッチ画像有効化
    add_theme_support( 'post-thumbnails');
    set_post_thumbnail_size( 150, 150, true );

    add_action( 'init', 'register_cpt_test_custom' );

    function register_cpt_test_custom() {

        $labels = array(
            'name' => _x( 'テスト項目一覧', 'test_custom' ),
            'singular_name' => _x( 'テスト項目一覧', 'test_custom' ),
            'add_new' => _x( 'テスト項目追加', 'test_custom' ),
            'add_new_item' => _x( 'Add New テスト項目', 'test_custom' ),
            'edit_item' => _x( 'テスト項目を編集', 'test_custom' ),
            'new_item' => _x( '新しいテスト', 'test_custom' ),
            'view_item' => _x( 'テスト項目の詳細を見る', 'test_custom' ),
            'search_items' => _x( 'テスト項目検索', 'test_custom' ),
            'not_found' => _x( 'テスト項目が見つかりません', 'test_custom' ),
            'not_found_in_trash' => _x( 'ゴミ箱にテスト項目はありません', 'test_custom' ),
            'parent_item_colon' => _x( '新テスト項目:', 'test_custom' ),
            'menu_name' => _x( 'テスト一覧', 'test_custom' ),
        );

        $args = array(
            'labels' => $labels,
            'hierarchical' => false,
            'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields' ),
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'show_in_nav_menus' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'has_archive' => true,
            'query_var' => true,
            'can_export' => true,
            'rewrite' => true,
            'capability_type' => 'post'
        );
        register_post_type( 'test_custom', $args );
    }

function add_menu_icons_styles(){
     echo '<style>
          #adminmenu #menu-posts-team div.wp-menu-image:before {
               content: "\f154";
          }
     </style>';
}
add_action( 'admin_head', 'add_menu_icons_styles' );

これでアイキャッチ画像も問題なく使えました。

カスタム投稿タイプをコードで作成する場合はちょっと注意した方がいいのかなぁと。
よくコードで作成してらっしゃる方のサイトとかですと、
『ここでアイキャッチ画像を追加するために以下のコードを追加します!』
てadd_theme_supportでカスタム投稿タイプを指定してますが……
元々アイキャッチ画像が使えるテーマでそれをやるのはどうもダメっぽい?

まぁテーマによるでしょうけど。

カスタム投稿タイプをコードで追加した結果、
『通常投稿でアイキャッチ画像が使えなくなったよ!?(;・∀・)』となった場合は、
ここんとこもちょっと疑った方が良さそうです。

マジックミラーって普通に売ってるんですねぇ(;・∀・)

 

コメントを残す

メールアドレスが公開されることはありません。

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)