質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

プラグイン

プラグイン(plug-in)は、ソフトウェアアプリケーションの機能拡張の為に開発された、一組のソフトウェアコンポーネントのことを指します。

Q&A

解決済

1回答

778閲覧

WordpressプラグインPost List Generatorにてカテゴリー別のリストを作りたい

hobbit

総合スコア11

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

プラグイン

プラグイン(plug-in)は、ソフトウェアアプリケーションの機能拡張の為に開発された、一組のソフトウェアコンポーネントのことを指します。

0グッド

0クリップ

投稿2017/12/12 04:52

wordpressプラグイン「Post List Generator」にてカテゴリーごとの投稿リストを複数作りたいと考えています。
「Post List Generator」には、カテゴリーのスラッグを設定できる機能がありますが、複数のリストを用意した場合、全部のリストに反映する為、つかえません。

そこで、ショートコードを[showpostlist test="aaa"]のようにして、配列を渡すことで、以下のように
条件分岐させ、これは、成功しました。

しかし、「続きをみる」という無限スクロールを推すと、このショートコードの配列が無視されます。
どのようにして引き継いでやればよいのかというところで、躓いています。

ヒントをご存知の方、宜しくお願いします。

php

1 //追加 2 extract(shortcode_atts(array( 3 'test' => '' 4 ), $atts)); 5 6 if ($atts == 'aaa'){ 7 $condition['category_name'] = "aaa" 8 }else{ 9 $condition['category_name'] = "bbb" 10 }

php

1post-list-generator.php 2<?php 3/* 4 Plugin Name: Post List Generator 5Plugin URI: http://residentbird.main.jp/bizplugin/ 6Description: 記事(投稿・固定ページの一覧を作成するプラグインです 7Version: 1.3.0 8Author:WordPress Biz Plugin 9Author URI: http://residentbird.main.jp/bizplugin/ 10*/ 11 12include_once( dirname(__FILE__)."/admin-ui.php" ); 13new PostListGeneratorPlugin(); 14 15class PLG{ 16 const VERSION = "1.3.0"; 17 const SHORTCODE = "showpostlist"; 18 const OPTIONS = "post_list_options"; 19 20 public static function get_option(){ 21 return get_option(self::OPTIONS); 22 } 23 24 public static function update_option( $options ){ 25 if ( empty($options)){ 26 return; 27 } 28 update_option(self::OPTIONS, $options); 29 } 30 31 public static function enqueue_css_js(){ 32 wp_enqueue_style('post-list-style', plugins_url('post-list-generator.css', __FILE__ ), array(), self::VERSION ); 33 wp_enqueue_script('post-list-js', plugins_url('next-page.js', __FILE__ ), array('jquery'), self::VERSION ); 34 } 35 36 public static function localize_js(){ 37 wp_localize_script( 'post-list-js', 'PLG_Setting', array( 38 'ajaxurl' => admin_url('admin-ajax.php'), 39 'action' => 'get_post_ajax', 40 "plg_dateformat" => "Y年n月j日", 41 'next_page' => '1', 42 )); 43 } 44} 45 46/** 47 * プラグイン本体 48 */ 49class PostListGeneratorPlugin{ 50 51 var $adminUi; 52 53 public function __construct(){ 54 register_activation_hook(__FILE__, array(&$this,'on_activation')); 55 add_action( 'admin_init', array(&$this,'on_admin_init') ); 56 add_action( 'admin_menu', array(&$this, 'on_admin_menu')); 57 add_action( 'wp_enqueue_scripts', array(&$this,'on_enqueue_sctipts') ); 58 add_action( 'wp_ajax_get_post_ajax', array(&$this,'get_post_ajax') ); 59 add_action( 'wp_ajax_nopriv_get_post_ajax', array(&$this,'get_post_ajax') ); 60 add_shortcode( PLG::SHORTCODE, array(&$this,'show_shortcode')); 61 } 62 63 function on_activation() { 64 $option = PLG::get_option(); 65 if( $option ) { 66 return; 67 } 68 $arr = array( 69 "content_type" => "投稿", 70 "orderby" => "公開日順", 71 "category_name" => "", 72 "numberposts" => "30", 73 'window_open' => 'false', 74 ); 75 PLG::update_option( $arr ); 76 } 77 78 function on_enqueue_sctipts() { 79 if ( is_admin() ) { 80 return; 81 } 82 PLG::enqueue_css_js(); 83 PLG::localize_js(); 84 } 85 86 function on_admin_init() { 87 $this->adminUi = new PLGAdminUi(__FILE__); 88 } 89 90 public function on_admin_menu() { 91 add_options_page("Post List設定", "Post List設定", 'administrator', __FILE__, array(&$this->adminUi, 'show_admin_page')); 92 } 93 94 function show_shortcode($atts){ 95 extract(shortcode_atts(array( 96 'test' => '' 97 ), $atts)); 98 $info = new PostListInfo( '',$test); 99 ob_start(); 100 include( dirname(__FILE__).'/post-list-view.php'); 101 $contents = ob_get_contents(); 102 ob_end_clean(); 103 return $contents; 104 } 105 106 /** 107 * Ajax 108 */ 109 function get_post_ajax(){ 110 $page = absint( $_REQUEST['page'] ); 111 if ( $page == 0){ 112 die(); 113 } 114 $info = new PostListInfo( $page ); 115 $info->next_page = $info->has_next ? $page + 1: null; 116 $json = json_encode( $info ); 117 $charset = get_bloginfo( 'charset' ); 118 nocache_headers(); 119 header( "Content-Type: application/json; charset=$charset" ); 120 echo $json; 121 die(); 122 } 123} 124 125 126class PostListInfo{ 127 var $items = array(); 128 var $has_next = false; 129 var $window_open = false; 130 131 public function __construct( $page = 0 , $atts){ 132 133 $options = PLG::get_option(); 134 $this->window_open = isset( $options['window_open'] ) ? $options['window_open'] : false; 135 136 $condition = array(); 137 if ( $options['content_type'] == '投稿'){ 138 $condition['post_type'] = 'post'; 139 }else if ( $options['content_type'] == '固定ページ' ){ 140 $condition['post_type'] = 'page'; 141 }else{ 142 $condition['post_type'] = array('page', 'post'); 143 } 144 if ( $options['orderby'] == '公開日順'){ 145 $condition['orderby'] = 'post_date'; 146 $condition['order'] = 'desc'; 147 }else if ( $options['orderby'] == '更新日順'){ 148 $condition['orderby'] = 'modified'; 149 $condition['order'] = 'desc'; 150 }else{ 151 $condition['orderby'] = 'title'; 152 $condition['order'] = 'asc'; 153 } 154 $condition['numberposts'] = $options['numberposts'] + 1; 155 $condition['offset'] = $page * $options['numberposts']; 156 //$condition['category_name'] = $options['category_name']; 157 158 //追加 159 extract(shortcode_atts(array( 160 'test' => '' 161 ), $atts)); 162 163 if ($atts == 'aaa'){ 164 $condition['category_name'] = "aaa" 165 }else{ 166 $condition['category_name'] = "bbb" 167 } 168 169 $posts = get_posts( $condition ); 170 171 if ( !is_array($posts) ){ 172 return; 173 } 174 175 if ( count($posts) > $options['numberposts']){ 176 $this->has_next = true; 177 array_pop ( $posts ); 178 } 179 180 foreach($posts as $post){ 181 $this->items[] = new PostListItem($post, $options); 182 } 183 } 184} 185 186class PostListItem{ 187 var $date; 188 var $title; 189 var $url; 190 191 public function __construct( $post, $options ){ 192 $raw_date = $options['orderby'] == '公開日順' ? $post->post_date : $post->post_modified; 193 $dateformat = empty($options['plg_dateformat']) ? "Y年n月j日" : $options['plg_dateformat']; 194 $this->date = date($dateformat, strtotime($raw_date)); 195 $this->title = esc_html( $post->post_title ); 196 $this->url = get_permalink($post->ID); 197 } 198} 199 200?>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

プラグインの修正方法ではありませんが、このプラグインを使ってみては?
WordPress Infinite Scroll – Ajax Load More
比較にならないほど高機能です。試してみてください。

投稿2017/12/12 14:15

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hobbit

2017/12/13 09:32

Ajax Load Moreにて、思った通りの動作を確認することができました! こちらを使用することにします。ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問