Skip to content

Instantly share code, notes, and snippets.

@miya0001
Created October 8, 2012 19:00
Show Gist options
  • Save miya0001/3854254 to your computer and use it in GitHub Desktop.
Save miya0001/3854254 to your computer and use it in GitHub Desktop.
アイキャッチ画像を記事内の好きな場所に挿入するためのショートコード
<?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