質問の閲覧ありがとうございます。
今回は、WordPressで最初から用意されている、コメントのページングに関するリライトルールを削除(または無視)する方法が知りたいのですが、調べてもわからなかったので質問させて頂きました。
###前提・実現したいこと
コメントのページングを管理する変数cpage=xxx
をcomment-page-xxx
に変更するリライトルールを無視したい
Rule | Rewrite | source |
---|---|---|
([^/]+)/comment-page-([0-9]{1,})/?$ | index.php?name=$matches[1]&cpage=$matches[2] | post |
###実装中のfunctions.php
php
1add_action('init', 'my_add_single_rewrite_endpoint'); 2function my_add_single_rewrite_endpoint() { 3 add_rewrite_endpoint('x1', EP_PERMALINK, 'y1'); 4 add_rewrite_endpoint('x2', EP_PERMALINK, 'y2'); 5 add_rewrite_endpoint('x3', EP_PERMALINK, 'y3'); 6}
実際に叩くURLはhttp://example.com/記事スラッグ/x1/y1/?cpage=xxx
になるのですが、cpage=1
の場合は期待通りコメントの1ページ目が表示されるのですが、2より大きい数値を指定するとWordPressのデフォルトのリライトルールによりhttp://example.com/記事スラッグ/x1/y1/comment-page-2/
になってしまいます。
自分で追加したリライトルールに則ってページングしたいため、cpage
を残したままにしたいです。
###補足情報(言語/FW/ツール等のバージョンなど)
WordPress 4.7.8
###追記1
目標のURL(例:2ページ目)
理想
http://example.com/記事スラッグ/x1/y1/?cpage=2
現状
http://example.com/記事スラッグ/x1/y1/comment-page-2/
下記my_add_single_rewrite2()追記後
http://example.com/記事スラッグ/x1/y1/?cpage=-2%2F
*現状の設定について
functions.php
php
1add_filter('init', 'my_add_single_rewrite1'); 2add_filter('init', 'my_add_single_rewrite2'); 3add_filter('init', 'my_add_single_rewrite3'); 4 5function my_add_single_rewrite1() { 6 add_rewrite_endpoint('x1', EP_PERMALINK, 'y1'); 7 add_rewrite_endpoint('x2', EP_PERMALINK, 'y2'); 8 add_rewrite_endpoint('x3', EP_PERMALINK, 'y3'); 9} 10 11function my_add_single_rewrite2() { 12 global $wp_rewrite; 13 $wp_rewrite->comments_pagination_base = '?cpage='; 14} 15 16function my_add_single_rewrite3( $array ) { 17 add_rewrite_rule('smart-custom-fields/[^/]+/attachment/([^/]+)/?cpage=([0-9]{1,})/?$', 'index.php?attachment=$matches[1]&cpage=$matches[2]', 'top'); 18 add_rewrite_rule('smart-custom-fields/([^/]+)/?cpage=([0-9]{1,})/?$', 'index.php?smart-custom-fields=$matches[1]&cpage=$matches[2]', 'top'); 19 add_rewrite_rule('smart-custom-fields/[^/]+/([^/]+)/?cpage=([0-9]{1,})/?$', ' index.php?attachment=$matches[1]&cpage=$matches[2]', 'top'); 20 add_rewrite_rule('media/[^/]+/attachment/([^/]+)/?cpage=([0-9]{1,})/?$', 'index.php?attachment=$matches[1]&cpage=$matches[2]', 'top'); 21 add_rewrite_rule('media/([^/]+)/?cpage=([0-9]{1,})/?$', 'index.php?media=$matches[1]&cpage=$matches[2]', 'top'); 22 add_rewrite_rule('media/[^/]+/([^/]+)/?cpage=([0-9]{1,})/?$', 'index.php?attachment=$matches[1]&cpage=$matches[2]', 'top'); 23 add_rewrite_rule('feature/[^/]+/attachment/([^/]+)/?cpage=([0-9]{1,})/?$', 'index.php?attachment=$matches[1]&cpage=$matches[2]', 'top'); 24 add_rewrite_rule('feature/([^/]+)/?cpage=([0-9]{1,})/?$', 'index.php?feature=$matches[1]&cpage=$matches[2]', 'top'); 25 add_rewrite_rule('feature/[^/]+/([^/]+)/?cpage=([0-9]{1,})/?$', 'index.php?attachment=$matches[1]&cpage=$matches[2]', 'top'); 26 add_rewrite_rule('.?.+?/attachment/([^/]+)/?cpage=([0-9]{1,})/?$', 'index.php?attachment=$matches[1]&cpage=$matches[2]', 'top'); 27 add_rewrite_rule('(.?.+?)/?cpage=([0-9]{1,})/?$', 'index.php?pagename=$matches[1]&cpage=$matches[2]', 'top'); 28 add_rewrite_rule('[^/]+/attachment/([^/]+)/?cpage=([0-9]{1,})/?$', 'index.php?attachment=$matches[1]&cpage=$matches[2]', 'top'); 29 add_rewrite_rule('([^/]+)/?cpage=([0-9]{1,})/?$', 'index.php?name=$matches[1]&cpage=$matches[2]', 'top'); 30 add_rewrite_rule('[^/]+/([^/]+)/?cpage=([0-9]{1,})/?$ ', 'index.php?attachment=$matches[1]&cpage=$matches[2] ', 'top'); 31};
###追記2
Debug Barで確認したページリクエスト
ローカル(MAMP)
Request: 記事スラッグ/x1/y1 Query String: name=記事スラッグ&cpage=2&y=y2 Matched Rewrite Rule: ([^/]+)/x1(/(.*))?/?$ Matched Rewrite Query: name=記事スラッグ&y=y1
サーバー(functions.phpに追加後)
Request: 記事スラッグ/x1/y1 Query String: name=記事スラッグ&cpage=-2%2F&y=y2 Matched Rewrite Rule: ([^/]+)/x1(/(.*))?/?$ Matched Rewrite Query: name=記事スラッグ&y=y1
サーバー(functions.phpに追加前)
Request: 記事スラッグ/x1/y1/comment-page-2 Query String: name=記事スラッグ&y=y1%2Fcomment-page-2 Matched Rewrite Rule: ([^/]+)/x1(/(.*))?/?$ Matched Rewrite Query: name=記事スラッグ&y=y1%2Fcomment-page-2
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/24 02:54
2018/01/24 03:19