カスタム投稿タイプのアーカイブページ

2011.03.03

カスタム投稿タイプ(カスタムポストタイプ)を使ってアーカイブページを作るとき、URLに『&post_type=投稿タイプ名』を負荷することで、パーマネントリンク設定をしていてもアーカイブを表示できる方法。(日本語フォーラム参照しました。)
下記をfunctions.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 )
    $add_link .= '&post_type=' . $my_archives_post_type;
	$link_html = preg_replace("/href=\'(.+)\'\s/","href='$1".$add_link."'",$link_html);

  return $link_html;
}

コメントは受け付けていません。