
タイトルが長すぎですが^^;
WordPress案件などでよく出てくるのが「特定のカテゴリーの記事だけサイドバーに表示させたい」というご要望です。
もちろん、サイドバーやトップページなど、テンプレート内に直接記述することは可能ですが、
今回はウィジェットとして追加作成してしまおうという話です。
メリットとしては、利用するテーマ内のウィジェットエリアがいくつかある場合、
自分で好きな場所に設定することが可能ということ。
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | /*---------------------------------------------------------------------------------*/ /* 特定カテゴリの最新の投稿タイトル一覧用サイドバーウィジェット */ ※当方がカスタマイズしているものです /*---------------------------------------------------------------------------------*/ // WP_Widget クラスを継承するサブクラスとして定義 class My_RecentPosts_Widget extends WP_Widget { // コンストラクタ function My_RecentPosts_Widget() { // ウィジェットの初期設定 $widget_opts = array ( 'classname' => 'my_recent_posts_widget' , 'description' => '特定カテゴリの最新の投稿タイトル一覧を表示。カテゴリーIDを指定してください。' ); $control_opts = array ( 'width' => 200, 'height' => 300); $this ->WP_Widget( 'MyRecentPostsWidget' , '特定のカテゴリー最近の投稿' , $widget_opts , $control_opts ); } // 管理画面にフォームを表示する処理 // $instance : 保存されているオプション値の連想配列 function form( $instance ) { // 設定が保存されていない場合はデフォルト値を設定 $instance = wp_parse_args(( array ) $instance , array ( 'title' => 'Recent Posts' , 'number' => 5, 'category' => 1)); // それぞれの設定値を取得 $title = strip_tags ( $instance [ 'title' ]); // タイトル $number = $instance [ 'number' ]; // 表示する投稿数 $category = $instance [ 'category' ]; // カテゴリID // フォーム用の name 属性および id 属性の値を取得 $titlename = $this ->get_field_name( 'title' ); $titleid = $this ->get_field_id( 'title' ); $numbername = $this ->get_field_name( 'number' ); $numberid = $this ->get_field_id( 'number' ); $categoryname = $this ->get_field_name( 'category' ); $categoryid = $this ->get_field_id( 'category' ); // フォームの出力 echo '<p><label for="' . $titleid . '">タイトル:</label><br />' ; echo '<input type="text" name="' . $titlename . '" id="' . $titleid . '" value="' . $title . '" style="width:100%;" /></p>' ; echo '<p><label for="' . $numberid . '">表示する投稿数:</label>' ; echo '<input type="text" name="' . $numbername . '" id="' . $numberid . '" value="' . $number . '" style="width:60px;" /></p>' ; echo '<p><label for="' . $categoryid . '">カテゴリーID:</label>' ; echo '<input type="text" name="' . $categoryname . '" id="' . $categoryid . '" value="' . $category . '" style="width:60px;" /></p>' ; } // 管理画面でオプションを保存する処理 // $new_instance : 入力されたオプション値の連想配列 // $old_instance : 保存されているオプション値の連想配列 // 戻り値 : 新たに保存されたオプション値の連想配列 function update( $new_instance , $old_instance ) { $instance [ 'title' ] = strip_tags ( $new_instance [ 'title' ]); $instance [ 'number' ] = $new_instance [ 'number' ]; $instance [ 'category' ] = $new_instance [ 'category' ]; // 例えばエラーチェックしたい場合は以下のような感じで if (! $instance [ 'title' ]) { // タイトルが空白の場合、元の値に戻してエラー表示 $instance [ 'title' ] = strip_tags ( $old_instance [ 'title' ]); $this ->m_error = '<span style="color:#ff0000;">タイトルが空白です。元の値に戻します。</span>' ; } return $instance ; } // サイトで表示する処理 // $args : 出力用文字列の連想配列 // $instance : 保存されているオプション値の連想配列 function widget( $args , $instance ) { extract( $args ); $instance = wp_parse_args(( array ) $instance , array ( 'title' => '' , 'number' => 5, 'category' => 1)); // タイトルはフィルタ処理 $title = $instance [ 'title' ]; $title = apply_filters( 'widget_title' , $title ); $number = $instance [ 'number' ]; $category = $instance [ 'category' ]; echo $before_widget . "n" ; echo $before_title . $title . $after_title . "n" ; echo '<ul class="recentEntries">' ; // 最新の投稿タイトルを件数とカテゴリを指定して取得&出力 query_posts( '&showposts=' . $number . '&cat=' . $category ); if (have_posts()) { while (have_posts()) { the_post(); echo '<li><a href="' .get_permalink(). '">' .the_title( '' , '' , false). '</a></li>' ; } } echo '</ul>' ; echo $after_widget . "n" ; // WordPress のループ用クエリをリセット wp_reset_query(); } } // ウィジェットを widgets_init フックで登録 add_action( 'widgets_init' , create_function( '' , 'return register_widget("My_RecentPosts_Widget");' )); |
メリットとしては、利用するテーマ内のウィジェットエリアがいくつかある場合、
自分で好きな場所に設定することが可能ということ。
上記のタグをfunction.phpに追記すると、オリジナルウィジェットが追加されます。
●カテゴリーの指定
●投稿数の指定
が可能です。(おそらく)コピペでいける(…はずです)ので、一度お試しくださいませ!!
