# カスタム投稿のカスタマイズ

- リンク: https://torounit.com/blog/%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e6%8a%95%e7%a8%bf%e3%81%ae%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%9e%e3%82%a4%e3%82%ba/
- 公開日: 2011-04-17T18:25:20+09:00
- 作成者: Toro\_Unit

カスタム投稿のパーマリンクはデフォルトだと

/カスタム投稿名/投稿タイトル

になってて、これが結構使いづらいんですよ。日本語タイトルだとURLまで日本語になって
しまったり。

また、月別アーカイブとかが無かったりで色々と面倒。なので、functions.phpで色々カスタマイズ
します。

2011-12-28追記：プラグインにしました。今後はこちらをお使い下さい。[Custom Post Type Permalinks](https://torounit.com/plugins/custom-post-type-permalinks/)

[php]
 class set_custom_rewrite { var $post_type;

 function set_custom_rewrite($post_type){
 $this->post_type = $post_type;

 add_action(‘init’, array(&$this,’set_rewrite’));
 add_action(‘init’, array(&$this,’
add_rule’)); add_filter(‘post_type_link’, array(&$this,’set_permalink’), 1, 3); }//
Rewrite Ruleの追加 function add_rule(){ add_rewrite_rule($this->post_type.’/([0-
9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$’,’index.php?year
=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type=’.$
this->post_type); add_rewrite_rule($this->post_type.’/([0-9]{4})/([0-9]{1,2})/([
0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$’,’index.php?year=$matches[1]&monthnum=$matches[
2]&day=$matches[3]&feed=$matches[4]&post_type=’.$this->post_type); add_rewrite_rule(
$this->post_type.’/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$’,’index.
php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]&post_type
=’.$this->post_type); add_rewrite_rule($this->post_type.’/([0-9]{4})/([0-9]{1,2})/([
0-9]{1,2})/?$’,’index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&
post_type=’.$this->post_type); add_rewrite_rule($this->post_type.’/([0-9]{4})/([
0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$’,’index.php?year=$matches[1]&monthnum
=$matches[2]&feed=$matches[3]&post_type=’.$this->post_type); add_rewrite_rule($this-
>post_type.’/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$’,’index.php?year
=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type=’.$this->post_type);
add_rewrite_rule($this->post_type.’/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$’,’
index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]&post_type=’.$this-
>post_type); add_rewrite_rule($this->post_type.’/([0-9]{4})/([0-9]{1,2})/?$’,’index.
php?year=$matches[1]&monthnum=$matches[2]&post_type=’.$this->post_type); add_rewrite_rule(
$this->post_type.’/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$’,’index.php?year=
$matches[1]&feed=$matches[2]&post_type=’.$this->post_type); add_rewrite_rule($this-
>post_type.’/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$’,’index.php?year=$matches[1]&
feed=$matches[2]&post_type=’.$this->post_type); add_rewrite_rule($this->post_type.’/([
0-9]{4})/page/?([0-9]{1,})/?$’,’index.php?year=$matches[1]&paged=$matches[2]&post_type
=’.$this->post_type); add_rewrite_rule($this->post_type.’/([0-9]{4})/?$’,’index.
php?year=$matches[1]&post_type=’.$this->post_type);

 }

 function set_rewrite() {
 global $wp_rewrite;

 $queryarg = ‘post_type=’.$this->post_type.’&p=’;
 $wp_rewrite->add_rewrite_tag(‘%’.
$this->post_type.’_id%’, ‘([^/]+)’,$queryarg); $wp_rewrite->add_permastruct($this-
>post_type, ‘/’.$this->post_type.’/%year%/%monthnum%/%day%/%’.$this->post_type.’
_id%/’, false); }

 function set_permalink($post_link, $id = 0) {
 global $wp_rewrite;

 $post = &get_post($id);
 if ( is_wp_error( $post ) ) return $post;

 $newlink = $wp_rewrite->get_extra_permastruct($this->post_type);
 $newlink = str_replace("%".
$this->post_type."%", $post->post_type, $newlink); $newlink = str_replace("%".$this-
>post_type."_id%", $post->ID, $newlink);

 $post_date = strtotime($post->post_date);
 $post_year = date("Y",$post_date); $
post_monthnum = date("m",$post_date); $post_day = date("d",$post_date);

 $newlink = str_replace("%year%",$post_year, $newlink);
 $newlink = str_replace("%
monthnum%",$post_monthnum, $newlink); $newlink = str_replace("%day%",$post_day, 
$newlink);

 $newlink = home_url(user_trailingslashit($newlink));
 return $newlink; }

}

$set_blog_permalink = new set_custom_rewrite("カスタム投稿名");

[/php]

こいつをfunctions.phpに書くと、
 /カスタム投稿名/年/月/日/投稿id に個別記事のURL
が変更されます。

また、add_ruleのところで
 カスタム投稿の月別アーカイブなどのURLを追加しています。
これで /カスタム投稿名/年/月 などで、月別アーカイブ等が表示されるようになります。

ついでに
 [http://ja.forums.wordpress.org/topic/6080](http://ja.forums.wordpress.org/topic/6080)
を参考に [php]

global $my_archives_post_type;
 add_filter( ‘getarchives_where’, ‘my_getarchives_where’,
10, 2 ); function my_getarchives_where( $where, $r ) { global $my_archives_post_type;
if ( isset($r[‘post_type’]) ) { $my_archives_post_type = $r[‘post_type’]; $where
= str_replace( ‘\’post\”, ‘\” . $r[‘post_type’] . ‘\”, $where ); } else { $my_archives_post_type
= ”; } return $where; } add_filter( ‘get_archives_link’, ‘my_get_archives_link’ );
function my_get_archives_link( $link_html ) { global $my_archives_post_type; if (”!
= $my_archives_post_type ) $my_archives_post_type; $blog_url = get_bloginfo("url");

 $link_html = str_replace($blog_url,$blog_url.’/’.$my_archives_post_type,$link_html);

 return $link_html;
 }

[/php]

を記述しておくと、
 [php] wp_get_archives(‘type=monthly&post_type=カスタム投稿名’);[/
php]

でカスタム投稿の月別アーカイブ一覧が表示されます。

ここまでするならDrupalとかで作った方が楽なんじゃないかとかは言わない約束です。

参考
 [http://ja.forums.wordpress.org/topic/6080](http://ja.forums.wordpress.org/topic/6080)
[http://www.phpfreaks.com/forums/index.php?PHPSESSID=ad52a9sjjre4bh7fue8uf5c4g4&topic=328834.0](http://www.phpfreaks.com/forums/index.php?PHPSESSID=ad52a9sjjre4bh7fue8uf5c4g4&topic=328834.0)
