詳細ページのナビゲーションに、その記事が所属しているカテゴリの記事一覧を表示させたいとき。
<ul id="product_<?php $cat = get_the_category(); $cat = $cat[1]; { echo $cat->slug; } ?>"> <?php foreach((get_the_category()) as $cat) { $cat_id = $cat->cat_ID ; break ; } $query = 'cat=' . $cat_id. '&showposts=10'; query_posts($query) ; ?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <li><a href="<?php the_permalink(); ?>"/><?php the_title()?></a></li> <?php endwhile; endif; ?> <?php wp_reset_query() ?> </ul>
しかしこれだと親カテゴリが同じ場合、子カテゴリが違っているものも表示されます。
子カテゴリで分けたい場合。
<ul id="product_<?php $cat = get_the_category(); $cat = $cat[1]; { echo $cat->slug; } ?>"> <?php foreach((get_the_category()) as $cat) { $cat_id = $category[1]->cat_ID ; break ; } $query = 'cat=' . $cat_id. '&showposts=10'; query_posts($query) ; ?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <li><a href="<?php the_permalink(); ?>"/><?php the_title()?></a></li> <?php endwhile; endif; ?> <?php wp_reset_query() ?> </ul>
ulのid属性に「product_記事のスラッグ名」が入ります。
これをdreamweaverのテンプレート内に持っていきたくて、jQueryのappendToで連れてったりしました。
ゴリ押し くコ:彡
カテゴリ毎にidも決まっているので割とスムーズにいけると思います。
パンくずナビなんかで子カテゴリの名前を表示させたい場合
<ul id="breadcrumb"><!-- --><li><a href="../../../../index.html">HOME</a></li><!-- --><li><a href="../../../../products.html">商品一覧</a></li><!-- --><li><?php $category = get_the_category(); ?><a href="<?php echo get_category_link($category[1]->cat_ID) ?>"><?php $cat = get_the_category(); $cat = $cat[1]; { echo $cat->cat_name; } ?></a></li><!-- --><li><?php the_title(); ?></li><!-- --></ul>
子カテゴリになる場合、$cat・$categoryの記述を$cat[1]・$category[1]に変更するといけます。