Last active
March 2, 2018 08:55
-
-
Save Tenderfeel/5b3213381f81d1ef02fc313cedc54a58 to your computer and use it in GitHub Desktop.
Wordpressのcustom-headerの画像でカルーセル作る
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* wordpressのcustom-headerの画像でカルーセル作る | |
* 画像1枚しか選択されてない場合はimgタグ出力 | |
*/ | |
function custom_header_carousel() { | |
/** | |
* register_default_headersの値、ヘッダー未設定の場合はNULL | |
* @type Array | |
*/ | |
global $_wp_default_headers; | |
$headers = array(); | |
$html = ''; | |
/** | |
* 'random-default-image' おすすめヘッダー(register_default_headersで登録したやつ)をランダム表示 の場合 | |
* 'random-uploaded-image' アップロード済みヘッターをランダム表示 の場合 | |
* 'remove-header' 画像未設定 | |
* 'http....' 画像単体選択だとupload先のURL | |
*/ | |
$header_image_mod = get_theme_mod( 'header_image', '' ); | |
//アップロード済みヘッダーをランダム表示がON | |
//@see https://developer.wordpress.org/reference/functions/_get_random_header_data/ | |
if ( 'random-uploaded-image' == $header_image_mod) { | |
/** | |
* @type Array | |
*/ | |
$headers = get_uploaded_header_images(); | |
} else if( preg_match("/^(?:http|https)/i", $header_image_mod) ) { //画像URLの場合は1枚選択状態 | |
/** | |
* @type Object | |
*/ | |
$headers = get_custom_header(); | |
} elseif ( ! empty( $_wp_default_headers ) ) { // register_default_headers で登録された画像がある && 画像表示する設定である | |
$headers = $_wp_default_headers; | |
} | |
if ( !empty($headers) && is_array( $headers ) ) { | |
if(count($headers) > 1) { | |
$html .= '<div class="swiper-container" id="custom-header">'; | |
$html .= '<div class="swiper-wrapper">'; | |
foreach($headers as $header) { | |
$html .= '<div class="swiper-slide">'; | |
$html .= create_header_img_tag((object) $header); | |
$html .= '</div>'; | |
} //endforeach | |
$html .= '</div>'; | |
$html .= '<div class="swiper-pagination"></div>'; | |
$html .= '<div class="swiper-button-prev"></div>'; | |
$html .= '<div class="swiper-button-next"></div>'; | |
$html .= '</div>'; | |
} else { | |
//@see https://developer.wordpress.org/reference/functions/get_custom_header_markup/ | |
$html .= sprintf( | |
'<div id="wp-custom-header" class="wp-custom-header">%s</div>', | |
create_header_img_tag((object) array_shift($headers)) | |
); | |
} | |
} elseif(is_object($headers)) { | |
$html .= sprintf( | |
'<div id="wp-custom-header" class="wp-custom-header">%s</div>', | |
create_header_img_tag($headers) | |
); | |
} | |
if ( strpos('random', $header_image_mod) !== false && is_header_video_active() && ( has_header_video() || is_customize_preview() ) ) { | |
wp_enqueue_script( 'wp-custom-header' ); | |
wp_localize_script( 'wp-custom-header', '_wpCustomHeaderSettings', get_header_video_settings()); | |
if(empty($html)) { | |
print '<div id="wp-custom-header" class="wp-custom-header"></div>'; | |
} | |
} | |
} | |
/** | |
* imgタグ生成 | |
* @param Object $header { | |
* ["url"]=> string | |
* ["width"]=> int | |
* ["height"]=> int | |
* ["video"]=> bool | |
* ["attachment_id"]=> int | |
* ["alt_text"]=> string | |
* ["description"]=> string | |
* } | |
* alt_textとdescriptionはどちらもaltに使用するがalt_textを優先する | |
*/ | |
function create_header_img_tag($header, $attr = array()) { | |
$url = sprintf( $header->url, get_template_directory_uri(), get_stylesheet_directory_uri() ); | |
if(property_exists($header, 'width')) { | |
$width = absint( $header->width ); //値を負ではない整数に変換 | |
} | |
if(property_exists($header, 'height')) { | |
$height = absint( $header->height ); | |
} | |
if(!array_key_exists('alt', $attr)) { | |
if( property_exists($header, 'alt_text')) { | |
$alt_text = $header->alt_text; | |
$attr['alt'] = $alt_text; | |
} elseif(property_exists($header, 'description')) { | |
$alt_text = $header->description; | |
$attr['alt'] = $alt_text; | |
} else { | |
$attr['alt'] = ''; | |
} | |
} | |
//このへん get_header_image_tag のソース | |
//@see https://developer.wordpress.org/reference/functions/get_header_image_tag/ | |
$attr = wp_parse_args( | |
$attr, | |
array( | |
'src' => $url, | |
'width' => $width, | |
'height' => $height | |
) | |
); | |
if(! empty( $header->attachment_id )) { //$_wp_default_headers の値だと attachment_id がない | |
$image_meta = get_post_meta( $header->attachment_id, '_wp_attachment_metadata', true ); | |
$size_array = array( $width, $height ); | |
if ( is_array( $image_meta ) ) { | |
$srcset = wp_calculate_image_srcset( $size_array, $header->url, $image_meta, $header->attachment_id ); | |
$sizes = ! empty( $attr['sizes'] ) ? $attr['sizes'] : wp_calculate_image_sizes( $size_array, $header->url, $image_meta, $header->attachment_id ); | |
if ( $srcset && $sizes ) { | |
$attr['srcset'] = $srcset; | |
$attr['sizes'] = $sizes; | |
} | |
} | |
} | |
$attr = array_map( 'esc_attr', $attr ); | |
$html = '<img'; | |
foreach ( $attr as $name => $value ) { | |
$html .= ' ' . $name . '="' . $value . '"'; | |
} | |
$html .= ' />'; | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment