Skip to content

Instantly share code, notes, and snippets.

@inc2734
Created January 16, 2015 05:42
Show Gist options
  • Save inc2734/6bc63c8fa27db64ea32a to your computer and use it in GitHub Desktop.
Save inc2734/6bc63c8fa27db64ea32a to your computer and use it in GitHub Desktop.
WordPress でサムネイルか、それが無いときのデフォルト画像を出し分けるテンプレートタグ
<?php
class Hoge {
/**
* get_the_post_thumbnail
* @param string $size
* @param array $attributes
* @return string
*/
public static function get_the_post_thumbnail( $size = 'post-thumbnail', array $attributes = array() ) {
if ( has_post_thumbnail() ) {
return get_the_post_thumbnail( get_the_ID(), $size, $attributes );
} else {
return sprintf( '<img src="%s/path/to/default-thumbnail.jpg" alt="" />', get_template_directory_uri() );
}
}
/**
* the_post_thumbnail
* @param string $size
* @param array $attributes
*/
public static function the_post_thumbnail( $size = 'post-thumbnail', array $attributes = array() ) {
echo self::get_the_post_thumbnail( $size, $attributes );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment