カテゴリー一覧は、WordPressのタグに用意されていますが、カテゴリー別記事一覧の表示はありませんね。当サイトのような記事やカテゴリーが多い場合は利用頻度は減ると思いますが、たまに利用したいというサイトがあります。
「get_categories」でカテゴリー情報を取得し、その数だけループさせれてカテゴリー別の一覧を表示しています。また、表示させたくないカテゴリーの指定と並びの順番も指定しています。
そして、カテゴリーの並び替えには「Category Order and Taxonomy Terms Order」を利用してください。
テーマのサイドバーやフッターで利用する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php $args = array ( 'order' => 'ASC' , // 並び順 'exclude' => 1 // 除外するカテゴリーID ); $categories = get_categories( $args ); foreach ( $categories as $category ) { echo '<div class="sidebar-wrapper">' ; // 表示スタイルをCSSで指定 echo '<h4>' . $category ->cat_name. '</h4>' ; echo '<ul>' ; // 表示数や並びを指定 query_posts( 'showposts=5&order=ASC&cat=' . $category ->cat_ID); if (have_posts()) { while (have_posts()) { the_post(); echo '<li><a href="' .get_permalink(). '">' .the_title( '' , '' , false). '</a></li>' ; } } echo '</ul>' ; echo '</div>' ; } ?> |
<div class=”sidebar-wrapper”> は当サイトの仕様ですので、サイトに合わせて変更が必要です。
ウィジェットで利用する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | class WP_Widget_CatArchives extends WP_Widget { function __construct() { $widget_ops = array ( 'classname' => 'Widget_CatArchives' , 'description' => 'カテゴリー毎に記事タイトルを表示するメニュー' ); parent::__construct( 'CatArchives' , 'サイドバー用:カテゴリー毎記事表示' , $widget_ops ); } function widget( $args , $instance ) { extract( $args ); $narabi = $instance [ 'narabi' ]; $number = $instance [ 'number' ]; $exclusion = $instance [ 'exclusion' ]; $r_posts = array ( 'order' => 'ASC' , 'exclude' => $exclusion ); $categories = get_categories( $r_posts ); foreach ( $categories as $category ) { echo $before_widget ; echo '<h4><a href="' . get_category_link( $category ->term_id ) . '"' . '>' . $category ->cat_name. '</a></h4>' ; echo '<ul>' ; query_posts( '&showposts=' . $number . '&order=' . $narabi . '&cat=' . $category ->cat_ID); if (have_posts()) { while (have_posts()) { the_post(); echo '<li><a href="' .get_permalink(). '">' .the_title( '' , '' , false). '</a></li>' ; } } echo '</ul>' ; echo $after_widget ; } } function update( $new_instance , $old_instance ) { $instance [ 'narabi' ] = $new_instance [ 'narabi' ]; $instance [ 'number' ] = (int) $new_instance [ 'number' ]; $instance [ 'exclusion' ] = $new_instance [ 'exclusion' ]; return $instance ; } function form( $instance ) { $instance = wp_parse_args( ( array ) $instance , array ( 'narabi' => 'DESC' , 'number' => 5) ); $narabi = $instance [ 'narabi' ]; // 並び順 $number = $instance [ 'number' ]; // 表示する投稿数 $exclusion = $instance [ 'exclusion' ]; // 除外カテゴリー // フォーム用の name 属性および id 属性の値を取得 $narabiname = $this ->get_field_name( 'narabi' ); $narabiid = $this ->get_field_id( 'narabi' ); $numbername = $this ->get_field_name( 'number' ); $numberid = $this ->get_field_id( 'number' ); $exclusionname = $this ->get_field_name( 'exclusion' ); $exclusionid = $this ->get_field_id( 'exclusion' ); // フォームの出力 echo '<p><label for="' . $numberid . '">表示する投稿数:</label>' ; echo '<input type="text" name="' . $numbername . '" id="' . $numberid . '" value="' . $number . '" style="width:60px;" /></p>' ; echo '<p><label for="' . $narabiid . '">表示順:</label>' ; echo '<select name="' . $narabiname . '" id="' . $narabiid . '" class="widefat">' ; echo '<option value="DESC"' .selected( $narabi , 'DESC' ). '>DESC:新しい順</option>' ; echo '<option value="ASC"' .selected( $narabi , 'ASC' ). '>ASC:古い順</option>' ; echo '</select></p>' ; echo '<p><label for="' . $narabiid . '">除外カテゴリー(例: 1,3)</label><br />' ; echo '<input type="text" name="' . $exclusionname . '" id="' . $exclusionid . '" value="' . $exclusion . '" style="width:100%;" /></p>' ; } } add_action( 'widgets_init' ,create_function( '' , 'return register_widget("WP_Widget_CatArchives");' )); |
上記をfunctions.phpに記述します。
他のウィジェットがある場合は、組み込みに注意が必要です。また、利用される場合は、自サイト用に併せて、ウィジェット領域の指定や見出し等を変更してください。
コメントを残す