カスタム投稿タイプのそれぞれの投稿のアイキャッチ画像を jQuery で表示するフォトギャラリーのメモ。
//taxonomy-works_cat.php
<?php get_header(); ?>
<div id="content">
<div class="works_top">
<?php $catinfo = get_term_by('slug', $term, $taxonomy); ?>
//カテゴリー名とその説明を表示
<h2 class="cat_title"><?php echo $catinfo->name; ?> : <?php echo $catinfo->description; ?></h2>  
</div><!-- end of .works_top -->
//このループで最初の画像と全てのサムネイル画像のデータを取得して出力
<div class="works_individual">
<?php if(have_posts()) : ?>
<?php $is_first = true; ?>  //最初の投稿かどうかのフラグ
<?php while(have_posts()): the_post(); ?>  
<?php if($is_first): ?>  //最初の投稿の場合中サイズの画像とタイトルを表示
<div class="works_photo">
<p class="main_photo"><a href="<?php the_permalink(); ?>" ><?php the_post_thumbnail('works_m'); ?></a></p>
<h3><?php the_title(); ?></h3>
</div><!-- end of .works_photo -->
<div class="works_right">
<div class="works_thumbnails">
<ul>
<?php endif; ?>  //全ての投稿のサムネイルを表示
<?php $is_first = false; ?>    //最初の投稿かどうかのフラグを false に
<li><a href="<?php the_permalink(); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail('works_s'); ?></a></li>
<?php endwhile; ?>
<?php endif;?>
</ul>
<?php pagenavi(); ?>
</div><!-- end of #works_thumbnails -->
</div><!-- end of .works_right -->
</div><!-- end of .works_individual -->
</div><!-- end of #content -->
<?php get_sidebar(); ?>
<?php get_footer('works'); ?>
<div id="works_data">
//2つ目のループで、画像の情報を取得。この部分は CSS で非表示にしておき、jQuery でデータを取得。
<?php if(have_posts()): while(have_posts()): the_post();  ?>
<div class="works_data_inner">
<div class="title_data"><?php the_title(); ?></div>
<div class="content_data"><?php the_content(); ?></div>
<div class="photo_data"><?php the_post_thumbnail('works_m'); ?></div>
<div class="link_data"><?php the_permalink(); ?></div>
<div class="h3_data"><h3><?php the_title(); ?></h3></div>
<?php endwhile; endif; ?>
</div><!-- end of .works_data -->
</body>
</html>
#works_data は非表示にしておく。
#works_data {
  display: none;
}
但し、body 要素の外側に記述すると HTML の文法エラーとなるので、body の閉じタグの前に記述するように調整する。他のフッターでは、get_footer() で body および html の閉じタグを含むが、このテンプレートでは get_footer(‘キーワード’)を利用するなどして body および html の閉じタグを含まないようにする。
以下は jQuery の記述。
jQuery(function($){
  var thumbnails$ = $('div.works_thumbnails ul li a');
  var thubnails_counts = thumbnails$.length;  
  var works_data_inner$ = $('#works_data div.works_data_inner');
    
  thumbnails$.click(function() {
    var that = $(this);
    var target = $(works_data_inner$.get(thumbnails$.index(this)));
    $('div.works_individual div.works_photo p a img').css('display', 'none').attr('src', target.find('img').attr('src')).fadeIn(700);
    $('div.works_individual div.works_photo p a').attr({href: target.find('div.link_data:first').text(), title: target.find('div.title_data:first').text()});
    $('div.works_individual div.works_photo h3').html(target.find('div.h3_data').html());
    return false;
  });
});