2009-04-28T14:58:01+09:00 [Tue]
--> [WordPress]
作業軽減策。
MD Comics Neoのページですが、やっつけ風味の仕様になっていた、画像のタグ入れ部分を、爆乳様(仮名)の負担を軽減するためと、ミスする隙を埋めるために、仕様変更しました。
取り合えず要求仕様を満たすために、それぞれのサイズのタグをカスタムフィールドに入れるという、非常に回りくどい手順になっていましたが、その部分をこちらとこちらを参考にして、勝手にやってくれるようにしました。もはや、「投稿に挿入」すら必要ありませんw。
/* functions.php*/
function get_the_post_image($postid,$size,$order=0,$max=null) {
$attachments = get_children(array(
'post_parent' => $postid,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
if ( is_array($attachments) ){
foreach ($attachments as $key => $row) {
$mo[$key] = $row->menu_order;
$aid[$key] = $row->ID;
}
array_multisort($mo, SORT_ASC,$aid,SORT_ASC,$attachments);
$max = empty($max)? $order+1 :$max;
for($i=$order;$i<$max;$i++){
$the_image = $attachments[$i];
$attachmenturl = wp_get_attachment_url($the_image->ID);
$attachmentimage = wp_get_attachment_image($the_image->ID, $size );
$img_title = $the_image->post_title;
$img_desc = $the_image->post_excerpt;
$img_alt = $the_image->post_name;
if ($size == "thumbnail" || $size == "medium"){
$attachmentimage = preg_replace('/class="attachment.*?"/','class="alignright size-'.$size.'"',$attachmentimage);//CSSに合わせるため
$attachmentimage = preg_replace('/alt=".*?"/','alt="'.attribute_escape($img_alt).'"',$attachmentimage);//altが直接拾えないので
echo '<a href="'.clean_url($attachmenturl).'" rel="lightbox['.get_the_ID().']" title="'.attribute_escape($img_title).'">'.$attachmentimage.'</a>';
} elseif($size == "large") {
$attachmentimage = preg_replace('/class="attachment.*?"/','class="alignright size-'.$size.'"',$attachmentimage);
$attachmentimage = preg_replace('/alt=".*?"/','alt="'.attribute_escape($img_alt).'"',$attachmentimage);
echo $attachmentimage;
} else {
echo '<img src="'.clean_url($attachmenturl).'">';
}
}
}
}
/* index.phpなどでの呼び出し部分*/
<?php get_the_post_image(get_the_ID(), 'thumbnail', 0, 1); ?>
/* get_the_post_image($post_id, $size, $order, $max)
$post_id= 記事ID
$size = 画像のサイズ(thumbnail,medium,large,full)
$order = ギャラリーでの順番
$max = $orderから何枚使うか
*WebTecNoteさんより拝借。*
*/
WebTecNoteさんから、記事に添付された画像をソートする部分を拝借して、Forumからデータを拾う部分をいただいて、くっつけただけのものですが……。
これで、扉ページと、個別ページと、リストページに、それぞれ、何もせずに予定どおりの画像が入るようにできました。
先人の皆さんに感謝。