デフォルトだとフロントページには最新の投稿が表示されますよね。
WordPressはそもそもブログを構築するためのソフトウェアですし文句言うつもりは毛頭ないですが、CMSという側面もあるのでフロントページに固定ページを指定するケースも多いです。
というかそればっかです。
でもそうするとさっきまでフロントページにあった投稿の一覧ページがどっかいっちゃうんですよね。
というわけで今回は、
https://www.560designs.com/・・・フロントページ(固定ページ)
https://www.560designs.com/news/・・・お知らせ一覧ページ(アーカイブページ)
こんな構成を実現する方法の紹介です。
当然のことながらカスタム投稿を使えば簡単なので、あえてビルトインの投稿を使いたいモノ好きな方向けです。
まず、アクションフック「registered_post_type」を使ってオブジェクト「$wp_post_types」を書き換えます。
add_action( 'registered_post_type', function () { global $wp_post_types; $wp_post_types['post']->has_archive = true; } );
これでアーカイブページを持てるようになりました。
次にパーマリンクを設定します。
やり方はいろいろありますが、今回はアクションフック「generate_rewrite_rules」を使ってゴリゴリ書きます。
function post_has_archive_rules() { global $wp_rewrite; $new_rules = array (); $new_rules['news/?$'] = 'index.php?post_type=post'; $new_rules['news/feed/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=post&feed=$matches[1]'; $new_rules['news/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=post&feed=$matches[1]'; $new_rules['news/page/?([0-9]{1,})/?$'] = 'index.php?post_type=post&paged=$matches[1]'; $new_rules['news/category(/([^/]+))+?(/page/([0-9]+))?/?$'] = 'index.php?post_type=post&category_name=$matches[2]&paged=$matches[4]'; $new_rules['news/tag(/([^/]+))+?(/page/([0-9]+))?/?$'] = 'index.php?post_type=post&tag=$matches[2]&paged=$matches[4]'; $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; } add_action( 'generate_rewrite_rules', 'post_has_archive_rules' );
次に日付アーカイブ。 フィルターフック「post_rewrite_rules」を使います。
function post_has_archive_date_rules( $post_rewrite ) { $add_post_rewrite = array (); foreach ( $post_rewrite as $regex => $rule ) $add_post_rewrite['news/' . $regex] = str_replace ( 'index.php?', 'index.php?post_type=post&', $rule ); $post_rewrite = array_merge ( $add_post_rewrite, $post_rewrite ); return $post_rewrite; } add_filter( 'post_rewrite_rules', 'post_has_archive_date_rules' );
他にカスタム投稿がない(投稿の日付アーカイブしか存在していない)ならばこちらでOKです。
心配ならゴリゴリ書いた方が確実です。
function post_has_archive_date_rules( $post_rewrite ) { $add_post_rewrite = array (); $add_post_rewrite['news/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/?$'] = 'index.php?post_type=post&attachment=$matches[1]'; $add_post_rewrite['news/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/trackback/?$'] = 'index.php?post_type=post&attachment=$matches[1]&tb=1'; $add_post_rewrite['news/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]'; $add_post_rewrite['news/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]'; $add_post_rewrite['news/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$'] = 'index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]'; $add_post_rewrite['news/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/embed/?$'] = 'index.php?post_type=post&attachment=$matches[1]&embed=true'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/embed/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&embed=true'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/trackback/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&tb=1'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&feed=$matches[5]'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&feed=$matches[5]'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/page/?([0-9]{1,})/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&paged=$matches[5]'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/comment-page-([0-9]{1,})/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&cpage=$matches[5]'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)(?:/([0-9]+))?/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&page=$matches[5]'; $add_post_rewrite['news/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/([^/]+)/?$'] = 'index.php?post_type=post&attachment=$matches[1]'; $add_post_rewrite['news/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/([^/]+)/trackback/?$'] = 'index.php?post_type=post&attachment=$matches[1]&tb=1'; $add_post_rewrite['news/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]'; $add_post_rewrite['news/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]'; $add_post_rewrite['news/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$'] = 'index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]'; $add_post_rewrite['news/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/([^/]+)/embed/?$'] = 'index.php?post_type=post&attachment=$matches[1]&embed=true'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/embed/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&embed=true'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/comment-page-([0-9]{1,})/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&cpage=$matches[4]'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/embed/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&embed=true'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/comment-page-([0-9]{1,})/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]&cpage=$matches[3]'; $add_post_rewrite['news/([0-9]{4})/([0-9]{1,2})/?$'] = 'index.php?post_type=post&year=$matches[1]&monthnum=$matches[2]'; $add_post_rewrite['news/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=post&year=$matches[1]&feed=$matches[2]'; $add_post_rewrite['news/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=post&year=$matches[1]&feed=$matches[2]'; $add_post_rewrite['news/([0-9]{4})/embed/?$'] = 'index.php?post_type=post&year=$matches[1]&embed=true'; $add_post_rewrite['news/([0-9]{4})/page/?([0-9]{1,})/?$'] = 'index.php?post_type=post&year=$matches[1]&paged=$matches[2]'; $add_post_rewrite['news/([0-9]{4})/comment-page-([0-9]{1,})/?$'] = 'index.php?post_type=post&year=$matches[1]&cpage=$matches[2]'; $add_post_rewrite['news/([0-9]{4})/?$'] = 'index.php?post_type=post&year=$matches[1]'; $post_rewrite = array_merge ( $add_post_rewrite, $post_rewrite ); return $post_rewrite; } add_filter( 'post_rewrite_rules', 'post_has_archive_date_rules' );
あとは「設定 > パーマリンク設定」にアクセスするなどして flush_rewrite_rules を実行すればOKです。