カテゴリーに記事一覧を表示させます。
ダッシュボード→外観→テーマ編集→カテゴリーテンプレート (category.php)
選択した25~49行目を削除する。
削除したところに以下のコードを挿入。
<?php
// 同じタグの記事を関連記事として表示する
$tags = wp_get_post_tags($post->ID, array('orderby'=>'rand')); // 複数タグを持つ場合ランダムで取得
if ($tags) {
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag), // タグのIDで記事を取得
// 'post__not_in' => array($post->ID), // 表示している記事を除く
'showposts'=>5, // 取得記事数
'caller_get_posts'=>1, // 取得した記事の何番目から表示するか
'orderby'=> 'date' // 記事を日付順にする
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?> <div class="termlist_one">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array(100,100) ); ?></a>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br>
<?php echo get_post_meta($post->ID, _aioseop_description, true); ?>
<br clear="all" />
</div><!-- .termlist_one --> <?php
endwhile;
}
wp_reset_query();
}
?>‘showposts’=>5, // 取得記事数 の部分は表示させたい記事数なのでお好みで。
ここのブログでは記事数が100以上あるので5→200に変更しました。
cssも以下のソースを貼り付けました。
.termlist_one {
padding-bottom: 2px;
margin-bottom: 2px;
}
.termlist_one img {
float: left;
margin-right: 5px;
}
以上でカテゴリーページに記事一覧が表示されました。
使用したソースはSalty WordPress様のコードを使用させていただきました。
ありがとうございました。
追記
表示している記事がないので、カテゴリーを全部表示したいので下記のように
コメントアウトしました。
‘post__not_in’ => array($post->ID), // 表示している記事を除く
// ‘post__not_in’ => array($post->ID), // 表示している記事を除く
注意
子カテゴリーを作成すると親カテゴリーの表示がうまくいかないので
子カテゴリーの作成はやめた方が無難です。
<参考リンク>
Salty WordPress
http://wp.salt26.jp/general/964
スポンサードリンク
