Written by Anonymous
function get_categorized_no_image_url($url, $width = null, $height = null, $id = null, $is_singular = true){
if (!$is_singular) {
return $url;
}
if (!$id) {
$id = get_the_ID();
}
//サイズ指定がある場合はカテゴリーURLを取得
if ($width && $height) {
//カスタムタクソノミのタームページに設定されているアイキャッチを取得する
$taxonomies = get_taxonomies();
//除外するタクソノミ
if ($taxonomies) {
$ex_taxonomies = [
'category',
'post_tag',
'nav_menu',
'link_category',
'post_format',
'wp_theme',
'wp_template_part_area',
];
// 各タクソノミ名を確認
foreach ( $taxonomies as $taxonomy ) {
$terms = get_the_terms( $id, $taxonomy );
//WordPressで一般的に使われるようなタクソノミは除外
if (!in_array($taxonomy, $ex_taxonomies)) {
//タームがある場合
if ( $terms && ! is_wp_error( $terms ) ) {
//各タームに対して
foreach ( $terms as $term ) {
$term_id = $term->term_id;
$term_url = get_the_tag_eye_catch_url($term_id);
if ($term_url) {
$url = get_image_sized_url($term_url, $width, $height);
if (!file_exists(url_to_local($url))) {/**/
$url = $term_url;/**/
}/**/
continue;
}
}
}
}
}
}
//タグページのアイキャッチ画像取得
//get_the_tags() 関数を使用して、現在の投稿に関連付けられたタグを取得
$tags = get_the_tags();
if ($tags && isset($tags[0])) {
$tag = $tags[0];
$tag_id = $tag->term_id;
$tag_url = get_the_tag_eye_catch_url($tag_id);
if ($tag_url) {
$url = get_image_sized_url($tag_url, $width, $height);
if (!file_exists(url_to_local($url))) {/**/
$url = $tag_url;/**/
}/**/
}
}
$cat_url = null;
$cat = get_the_category($id);
//カテゴリページのアイキャッチ画像取得
if ($cat && isset($cat[0])) {
$cat_url = get_the_category_eye_catch_url($cat[0]->cat_ID);
if ($cat_url) {
$url = get_image_sized_url($cat_url, $width, $height);
if (!file_exists(url_to_local($url))) {/**/
$url = $cat_url;/**/
}/**/
}
}
//メインカテゴリーが設定してある場合
$main_cat_id = get_the_page_main_category($id);
if ($main_cat_id && in_category($main_cat_id, $id)) {
$cat_url = get_the_category_eye_catch_url($main_cat_id);
if ($cat_url) {
$url = get_image_sized_url($cat_url, $width, $height);
if (!file_exists(url_to_local($url))) { /**/
$url = $cat_url;/**/
}/**/
}
}
}
return $url;
}