[WordPress] 投稿・固定ページ・カスタムフィールドに紐づく画像を取得・表示するためのオレオレ関数

<?php/** * 投稿に紐づく画像を1点表示する */function oc_the_image($post_ID = 0, $size = "large", $attr = "", $icon = false, $custom_field_key = "image", $primary_img_id = "", $notfound_img = "notfound.png") { echo oc_get_the_image($post_ID, $size, $attr, $icon, $custom_field_key, $primary_img_id, $notfound_img);}/** * 投稿に紐づく画像のHTMLコードを取得する */function oc_get_the_image($post_ID = 0, $size = "large", $attr = "", $icon = false, $custom_field_key = "image", $primary_img_id = "", $notfound_img = "notfound.png") { $content = ""; $key = oc_get_pict_key($post_ID, $custom_field_key, $primary_img_id); $img = oc_get_pict_src($key, $size, $icon, $notfound_img); if($img) { $content = '<img src="'. $img[0] .'" width="' . $img[1] . '" height="' . $img[2] . '" ' . $attr . ' />'; } return $content;}/** * 投稿に紐づくカスタムフィールドに指定した画像を1点取得する * - カスタムフィールドには画像IDが格納されている前提での処理です。 */function oc_get_cf_img_url( $post_id, $custom_field_key ) { $imgid = get_post_meta($post_id,$custom_field_key,true); if($imgid) { $img = oc_get_pict_src($imgid, "full", false, "notfound.png"); if($img) { return $img[0]; } } return "";}/** * 投稿に紐づく画像IDを取得する */function oc_get_pict_key($post_ID = 0, $custom_field_key = "image", $primary_img_id = "") { // 投稿IDが0なら現在の投稿IDを使用する if($post_ID == 0) { global $post; $post_ID = $post->ID; } // ------------------------------------------------- // 優先表示画像を表示する // ------------------------------------------------- if ($primary_img_id != "") { $key = $primary_img_id; // ------------------------------------------------- // アイキャッチ画像があれば表示する // ------------------------------------------------- } elseif ( function_exists("has_post_thumbnail") && has_post_thumbnail($post_ID)) { $key = get_post_meta($post_ID, '_thumbnail_id', true); // ------------------------------------------------- // カスタムフィールドに登録された最初の画像を使う // ------------------------------------------------- } else { $key = get_post_meta($post_ID, $custom_field_key, true); } // ------------------------------------------------- // ココまでに画像IDが取得できなければ // 記事に登録された最初の画像を使う // ------------------------------------------------- if (empty($key) || $key == 0 ){ $args = array( 'post_parent' => $post_ID, 'post_type' => 'attachment', 'post_mime_type' => 'image'); $files = get_children($args); $img_src = ""; if (!empty($files)){ $keys = array_keys($files); $key = $keys[0]; // 最初の写真を使う } } return $key;}/** * 画像の情報(URL、幅、高さ)を取得する */function oc_get_pict_src($key, $size, $icon=false, $notfound_img = "notfound.png") { $img = array(); // 画像IDが取得されていれば画像を表示 if (!empty($key) && $key != 0 ){ $img = wp_get_attachment_image_src($key, $size, $icon); } else { // ------------------------------------------------- // デフォルト画像を使用する // ------------------------------------------------- // それでもなければ、NOT FOUND画像を使う $notfound_path = get_stylesheet_directory_uri()."/images/common/".$notfound_img; // ココは適宜書き換え $width = 100; $height = 100; global $_wp_additional_image_sizes; // 配列(縦、横)でサイズを指定された場合 if(is_array( $size ) && count( $size ) >= 2 ) { $width = $size[0]; $height = $size[1]; // 文字列('thumbnail'等)でサイズを指定された場合 } elseif (is_string($size) && !empty($size) && !empty($_wp_additional_image_sizes[$size]) ) { $tumb_size = $_wp_additional_image_sizes[$size]; $width = $tumb_size["width"]; $height = $tumb_size["height"]; // 該当なし } else { // 初期値のまま表示 } $img[0] = $notfound_path; $img[1] = $width; $img[2] = $height; } return $img; }