固定ページとかindexで投稿一覧作るとき、
直近n件とか決まった数を表示するしかないとき、
別ページでいいからバックナンバーをずらっと並べたいなーってとき、
にいい塩梅のプラグインを見つけたんだけど、
そのままじゃちょっと具合が悪いとき、
のカスタマイズ
Post List Generator
環境
- wordpress4.2.2
- Post List Generator1.3.0
特徴
- tableで組まれてる
- 表示されるのは日付とタイトル
プラグインの設定もシンプルで、便利なような、でも物足りないような。
本文を追加する
標準機能としてサムネとか抜粋文表示のオンオフとか順序入れ替えが付いてればいいのに、って思うんだけどね。
ないからプラグイン自体をいじくることにする。
開く
WPのダッシュボード上で編集できるので
プラグイン>インストール済みプラグイン>Post List Generator>編集
とか、
プラグイン>プラグイン編集>編集するプラグインを選択[Post List Generator]>選択
でもってプラグイン一覧を出す。
これ。
宣言
post-list-generator/post-list-generator.php
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | <?php /* Plugin Name: Post List Generator Plugin URI: http://residentbird.main.jp/bizplugin/ Description: 記事(投稿・固定ページの一覧を作成するプラグインです Version: 1.3.0 Author:WordPress Biz Plugin Author URI: http://residentbird.main.jp/bizplugin/ */ include_once( dirname(__FILE__)."/admin-ui.php" ); new PostListGeneratorPlugin(); class PLG{ const VERSION = "1.3.0"; const SHORTCODE = "showpostlist"; const OPTIONS = "post_list_options"; public static function get_option(){ return get_option(self::OPTIONS); } public static function update_option( $options ){ if ( empty($options)){ return; } update_option(self::OPTIONS, $options); } public static function enqueue_css_js(){ wp_enqueue_style('post-list-style', plugins_url('post-list-generator.css', __FILE__ ), array(), self::VERSION ); wp_enqueue_script('post-list-js', plugins_url('next-page.js', __FILE__ ), array('jquery'), self::VERSION ); } public static function localize_js(){ wp_localize_script( 'post-list-js', 'PLG_Setting', array( 'ajaxurl' => admin_url('admin-ajax.php'), 'action' => 'get_post_ajax', "plg_dateformat" => "Y年n月j日", 'next_page' => '1', )); } } /** * プラグイン本体 */ class PostListGeneratorPlugin{ var $adminUi; public function __construct(){ register_activation_hook(__FILE__, array(&$this,'on_activation')); add_action( 'admin_init', array(&$this,'on_admin_init') ); add_action( 'admin_menu', array(&$this, 'on_admin_menu')); add_action( 'wp_enqueue_scripts', array(&$this,'on_enqueue_sctipts') ); add_action( 'wp_ajax_get_post_ajax', array(&$this,'get_post_ajax') ); add_action( 'wp_ajax_nopriv_get_post_ajax', array(&$this,'get_post_ajax') ); add_shortcode( PLG::SHORTCODE, array(&$this,'show_shortcode')); } function on_activation() { $option = PLG::get_option(); if( $option ) { return; } $arr = array( "content_type" => "投稿", "orderby" => "公開日順", "category_name" => "", "numberposts" => "30", 'window_open' => 'false', ); PLG::update_option( $arr ); } function on_enqueue_sctipts() { if ( is_admin() ) { return; } PLG::enqueue_css_js(); PLG::localize_js(); } function on_admin_init() { $this->adminUi = new PLGAdminUi(__FILE__); } public function on_admin_menu() { add_options_page("Post List設定", "Post List設定", 'administrator', __FILE__, array(&$this->adminUi, 'show_admin_page')); } /** * shortcode */ function show_shortcode(){ $info = new PostListInfo(); ob_start(); include( dirname(__FILE__).'/post-list-view.php'); $contents = ob_get_contents(); ob_end_clean(); return $contents; } /** * Ajax */ function get_post_ajax(){ $page = absint( $_REQUEST['page'] ); if ( $page == 0){ die(); } $info = new PostListInfo( $page ); $info->next_page = $info->has_next ? $page + 1: null; $json = json_encode( $info ); $charset = get_bloginfo( 'charset' ); nocache_headers(); header( "Content-Type: application/json; charset=$charset" ); echo $json; die(); } } class PostListInfo{ var $items = array(); var $has_next = false; var $window_open = false; public function __construct( $page = 0){ $options = PLG::get_option(); $this->window_open = isset( $options['window_open'] ) ? $options['window_open'] : false; $condition = array(); if ( $options['content_type'] == '投稿'){ $condition['post_type'] = 'post'; }else if ( $options['content_type'] == '固定ページ' ){ $condition['post_type'] = 'page'; }else{ $condition['post_type'] = array('page', 'post'); } if ( $options['orderby'] == '公開日順'){ $condition['orderby'] = 'post_date'; $condition['order'] = 'desc'; }else if ( $options['orderby'] == '更新日順'){ $condition['orderby'] = 'modified'; $condition['order'] = 'desc'; }else{ $condition['orderby'] = 'title'; $condition['order'] = 'asc'; } $condition['numberposts'] = $options['numberposts'] + 1; $condition['offset'] = $page * $options['numberposts']; $condition['category_name'] = $options['category_name']; $posts = get_posts( $condition ); if ( !is_array($posts) ){ return; } if ( count($posts) > $options['numberposts']){ $this->has_next = true; array_pop ( $posts ); } foreach($posts as $post){ $this->items[] = new PostListItem($post, $options); } } } class PostListItem{ var $date; var $title; var $url; /* ↓追加↓ */ var $content; /* ↑追加↑ */ public function __construct( $post, $options ){ $raw_date = $options['orderby'] == '公開日順' ? $post->post_date : $post->post_modified; $dateformat = empty($options['plg_dateformat']) ? "Y年n月j日" : $options['plg_dateformat']; $this->date = date($dateformat, strtotime($raw_date)); $this->title = esc_html( $post->post_title ); $this->url = get_permalink($post->ID); /* ↓追加↓ */ $this->content = esc_html( $post->post_content ); /* ↑追加↑ */ } } ?> |
元に倣って追加する。
コメントアウトしてるけど177~179、187~189行目が追加部分。
188行目を弄くれば表示内容をカスタマイズできる。
例えば、文字数制限したければこんなかんじにするとか↓
188 | $this->content = esc_html( mb_substr(strip_tags(apply_filters('the_content', $post->post_content)), 0, 80)); |
表示内容に追加
post-list-generator/post-list-view.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <div class='post-list'> <table class="post-table"> <?php foreach($info->items as $item): ?> <tr> <td class="postdate"><?php echo $item->date; ?> </td> <td ><a href="<?php echo $item->url; ?> "<?php if ( $info->window_open ){ echo 'target="_blank" '; } ?> > <?php echo $item->title; ?></a> </td> <!-- ↓追加↓ --> <td><?php echo $item->content; ?></td> <!-- ↑追加↑ --> </tr> <?php endforeach; ?> </table> <?php if ( $info->has_next ): ?> <p id='next-post-btn' >続きを見る</p> <div id='loader'><img src='<?php echo( plugins_url( "image/loader.gif", __FILE__ ));?>' /></div> <?php endif; ?> </div> |
もう見たまま。
上で宣言した変数名をぶっこむ。
「続きを見る」部分
プラグインの設定画面で表示件数を指定できる。
記事数が表示件数を超過した場合は「続きを見る」ボタンが出現し、
クリックすると超過分が表示される。
それはjsで制御されてる。
post-list-generator/next-page.js※36行目から抜粋
36 37 38 39 40 | for ( var i = 0; i < items.length; i++){ html += '<tr><td class="postdate">' + items[i].date + '</td>' + '<td><a href="' + items[i].url + '"' + target + '>' + items[i].title + '</a></td></tr>'; } html += '</table>'; |
これを
36 37 38 39 40 41 | for ( var i = 0; i < items.length; i++){ html += '<tr><td class="postdate">' + items[i].date + '</td>' + '<td><a href="' + items[i].url + '"' + target + '>' + items[i].title + '</a></td>' + '<td>' + items[i].content + '</td></tr>'; } html += '</table>'; |
こう
最初に宣言した変数名でもって、次に組んだtableと同じ構成を維持するように追加する。
元々表示させてる分と後発の分は別tableで生成されるので、
実際に動作させた時、各tableの中身によりtdの幅が変わってしまうことがある。
だから記事の文字数とか、もしくはtdのスタイルを指定して幅を決めてやることで回避しないといけない。
難点
一つのプラグインで一つのリストしかつくれない
プラグイン作ったことないけど別名義で複製すればいいのかね
コメント