画像を表示するショートコード。
<?php
/**
* テンプレートディレクトリの画像を利用するショートコード。
* @attribute src テンプレートディレクトリ以降の画像パス。先頭に/を含める
* @attribute alt 代替文字
* @attribute id id名
* @attribute class クラス名
* @attribute width 幅 pxなし
* @attribute height 高さ pxなし
* @attribute type tagならimgタグを返す。 pathならパスを返す。
* 属性がない場合は下記のHTMLコードを返す。
* <img src="%TEMPLATE_DIRECTORY%/screenshot.png" alt="*" />
* %TEMPLATE_DIRECTORY%はbloginfo('stylesheet_directory')
* 使い方[img src="/foo/bar.jpg" alt="×××"]
*/
function img_func($atts) {
$path = get_bloginfo('stylesheet_directory');
extract(shortcode_atts(array(
'src' => '/screenshot.png',
'alt' => '*',
'id' => '',
'class' => '',
'width' => '',
'height' => '',
'type' => "tag"
), $atts));
$path .= $src;
if ($type == "tag") {
$value = '<img src="' . $path . '" alt="' . $alt;
if ($id) {
$value .= '" id="' . $id;
}
if ($class) {
$value .= '" class="' . $class;
}
if ($width) {
$value .= '" width="' . $width;
}
if ($height) {
$value .= '" height="' . $height;
}
$value .= '" />';
} else if ($type == "path") {
$value = $path;
} else {
// 通常はここは実行されない
$value = '<img src="' . $path . '" alt="' . $alt;
if ($width) {
$value .= '" width="' . $width;
}
if ($height) {
$value .= '" height="' . $height;
}
$value .= '" />';
}
return $value;
}
add_shortcode('img', 'img_func');
?>
» Gist
No comments yet.
改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。