LoginSignup
4
3

More than 5 years have passed since last update.

wordpressのURLカスタマイズ

Last updated at Posted at 2013-08-26

/カスタム投稿名/投稿名/関連投稿名

というURLを作りたい。

異なるカスタム投稿間で親子関係的なものが定義できれば良いのだけど、異種投稿間では無理そう。そこで、URLだけそれっぽくして、single.php内で、関連投稿名の値によってベースとなる投稿情報のに加えて取得する情報を分岐させる構成を考えてみた。

加えたルールは

add_rewrite_rule('カスタム投稿名/([^/]+)/関連投稿名/?$','index.php?カスタム投稿名=$matches[1]&xxxpage=関連投稿ページ名','top');

xxxpageのxxx部分はわかりやすい値に。私はカスタム投稿名にした。

ただ、これだけではxxxpage部分は$wp_query->query_varsには格納されない。

query_varsフィルターに以下の関数を追加。

function add_query( $query_args ) {
  $query_args[] = "xxxpage";
  return $query_args;
}

これで、parse_requestメソッド実行時にドロップされずに、$query_varsで参照できる。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3