Created
October 8, 2012 19:00
-
-
Save miya0001/3854254 to your computer and use it in GitHub Desktop.
アイキャッチ画像を記事内の好きな場所に挿入するためのショートコード
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
<?php | |
function post_thumbnail_shortcode($p) { | |
$id = 0; | |
if (isset($p[0]) && $p[0]) { | |
$id = url_to_postid($p[0]); | |
} | |
if (!intval($id)) { | |
$id = get_the_ID(); | |
} | |
$title = esc_html(get_the_title($id)); | |
$attr = array( | |
"alt" => $title, | |
"title" => $title, | |
"class" => "thumbnail" | |
); | |
if (has_post_thumbnail($id)) { | |
if (isset($p['size']) && $p['size']) { | |
$size = $p['size']; | |
} else { | |
$size = 'medium'; | |
} | |
return get_the_post_thumbnail($id, $size, $attr); | |
} else { | |
return sprintf( | |
'<img width="300" height="225" src="http://example.com/sample-image.png" class="thumbnail wp-post-image" alt="%1$s" title="%1$s" />', | |
$title | |
); | |
} | |
} | |
add_shortcode("thumbnail", "post_thumbnail_shortcode"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment