//==============================================================================//
// function my_wp_get_attachment_link(): source file [ post-template.php ] //
//==============================================================================//
function my_wp_get_attachment_link( $id = 0,
$size = 'thumbnail',
$display = 'standard',
$gid = '' ) {
global $post;
// アッタチメント画像のデータベースレコードを取得する
$_post = & get_post( intval($id) );
// post_type がアタッチメントではないか又は URL 情報(guid) を取得できなかった場合は
// エラーメッセージを出力する
if ( ('attachment' != $_post->post_type) ||
!$url = wp_get_attachment_url($_post->ID) ) {
return __('Missing Attachment');
}
// タイトルの取得
$image_title = attribute_escape($_post->post_title);
// キャプションの取得(データベース上のフィールド名は post_excerpt )
$image_caption = attribute_escape($_post->post_excerpt);
// 説明の取得(データベース上のフィールド名は post_content )
$image_description = attribute_escape($_post->post_content);
// タイトルから [ ] の中身のテキストを取り出す
$groups_text = strstr( $image_title, "[" );
if ( $groups_text ) {
$groups_text = str_replace( "[", "", $groups_text );
$groups_text = str_replace( "]", "", $groups_text );
$groups_text = trim( $groups_text );
}
// グループの判定 (超単純版)
//
// AND, OR 検索や複数のグループの検索などを行うには構文解析などを用いて
// もっと複雑な判定処理が必要になる.
// ここでは、単純にグループ指定の文字列がイメージタイトル中に現れるかどうかだけを
// 判定している
if ( $gid ) {
// グループ指定あり
$regexp = trim( $gid );
if ( $groups_text ) {
// タイトルにグループ属性の記述がある
if ( ! ereg( $regexp, $groups_text ) ) {
// 一致するグループが存在しない
return ''; // スキップする
}
} else {
return ''; // スキップする
} // end of [ if ( $groups_text ) ]
} // end of [ if ( $gid ) ]
// wp_get_attachment_image() は alt属性タグを空で返すので、
// 自分で alt属性を付加する
$link_text = wp_get_attachment_image( $id, $size );
$alt_text = 'alt="' . $image_caption . '"';
$thumb_link = str_replace( 'alt=""', $alt_text, $link_text );
//
// 画像の表示形式に応じたリンク情報を作成する
//
if ( $display == 'lightbox' ) {
// Lightbox 2.0 用のリンクを作成する
$lb2group = trim( $gid );
if ( $lb2group ) {
return "<a href='$url' rel='lightbox[$lb2group]' title='$image_description'>$thumb_link</a>";
} else {
return "<a href='$url' rel='lightbox[post-$post->ID]' title='$image_description'>$thumb_link</a>";
}
} else {
// $display を指定しない場合は、画像のアップロード先のURL(guid) ではなく、
// URLを独自のパーマリンク形式 [ http://yyy.zzz/attachment/xxx ]
// で出力する
//
$url = get_attachment_link($_post->ID);
return "<a href='$url' title='$image_description'>$thumb_link</a>";
}
//return "<a href='$url' title='$image_title'></a>";
}
//==============================================================================//
// function mygallery_shortcode(): source file [ media.php ] //
//==============================================================================//
function mygallery_shortcode( $attr ) {
global $post;
// Allow plugins/themes to override the default gallery template.
// 他のプラグインやテーマがデフォルトの [gallery] をオーバライドできるようにする
//
// (今回は [gallery] とは別の名前で作成しているのでオーバライドの事は考えなくて良い)
//$output = apply_filters( 'post_gallery', '', $attr);
//if ( $output != '' )
// return $output;
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
// 並び順(orderby)の指示に対して正しい指定かどうかチェック
//【 Wordpress2.5.1 で加えられたセキュリティー対策コードです. 】
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
// [gallery] の各種オプションを 配列 $attr に格納する
extract( shortcode_atts( array(
'orderby' => 'menu_order ASC, ID ASC',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'display' => 'standard', // 今回追加したオプション(画像表示の方法)
'gid' => '', // 今回追加したオプション(グループ指定)
), $attr ) );
$id = intval($id);
$attachments = get_children("post_parent=$id&post_type=attachment&post_mime_type=image&orderby={$orderby}");
if ( empty($attachments) )
return ''; // アタッチメント画像データがなければ何もしない
if ( is_feed() ) { // 記事が feed の場合そのままアタッチメント画像データのリンクを出力
$output = "\n";
foreach ( $attachments as $id => $attachment )
$output .= wp_get_attachment_link( $id, $size, true ) . "\n";
return $output;
}
//$listtag = tag_escape($listtag); // 過去の遺物(ゴミ)です コメントアウトしましょう
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$columns = intval($columns); // サムネイル画像のカラム数
$itemwidth = $columns > 0 ? floor(100/$columns) : 100; // サムネイル画像の幅(%指定)
// ギャラリー用のスタイル(CSS) を本体(body) 中に埋め込む
//
// 複数のギャラリーを別々のスタイルを指定できるように、クラス名を毎回変更させている
//
$randomvalue = (double) microtime() * 1000000;
$myClassString = "mygallery" . $randomvalue;
$output = apply_filters('gallery_style', "
<style type='text/css'>
.$myClassString {
margin: auto;
}
.$myClassString-item {
float: left;
margin-top: 10px;
text-align: center;
width: {$itemwidth}%; }
.$myClassString img {
border: 2px solid #cfcfcf;
}
.$myClassString-caption {
margin-left: 0;
}
</style>
<!-- see mygallery_shortcode() in wp-includes/media.php -->
<div class='$myClassString'>");
// 記事に関連するアタッチメント画像のリンク情報(パーマリンク)を取得し
// 指定されたタグ itemtag, captiontag を付加する
foreach ( $attachments as $id => $attachment ) {
// カスタマイズされたリンクを取得する
$link = my_wp_get_attachment_link( $id, $size, $display, $gid );
if ( ! $link ) continue; // リンクが NULL の時は何もしないで次へ
$output .= "<{$itemtag} class='$myClassString-item'>";
$output .= "
<{$icontag} class='$myClassString-icon'>
$link
</{$icontag}>";
// アッタチメント画像に excerpt 情報が有ればキャプション欄にその内容を
// 表示する
//
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class='$myClassString-caption'>
{$attachment->post_excerpt}
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
// サムネイル画像のカラム送り
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';
}
// 最終カラムで clear: both でフロートを解除する
$output .= "
<br style='clear: both;' />
</div>\n";
// 最終的な HTMLタグを出力する
return $output;
}
// ショートコード [mygallery] を登録する
add_shortcode( 'mygallery', 'mygallery_shortcode' );