2024-12-30
Shifter で作ったサイトにカスタム投稿タイプのアーカイブを追加する方法
Shifter で作ったサイトでカスタム投稿タイプの年別や月別アーカイブを利用するにはひと手間かける必要があります。
静的生成する前は何事もなく閲覧できるので、気づかずにそのまま公開してしまうことがあります。
参考: Appending URLs to the artifacts | Shifter Documentation
参考にしたのは Sample Code B のところ。
サンプルではアメリカの州のリストを foreach
で $urls
に入れています。
これと同じように該当のアーカイブを $urls
に入れていけばアーカイブが生成されます。
今回は年別アーカイブが生成されるようにしています。
今回は Shifter Local を使ってローカル環境で動作チェックをしています。
投稿タイプのスラッグは add-type
としています。
Shifter Local の参考サイト
Shifter Local Explained | Shifter Documentation
Shifter-LocalでWordPressテーマ開発環境を作る
function append_archive_urls( $urls ) {
/**
* 年別アーカイブのリンクを取得
*
* wp_get_archives() – Function | Developer.WordPress.org
* https://developer.wordpress.org/reference/functions/wp_get_archives/
*/
$args = array(
'type' => 'yearly',
'post_type' => 'add-type',
'echo' => 0,
);
$yearly_list = wp_get_archives( $args );
if ( ! empty( $yearly_list ) ) {
// URL の配列を取得
preg_match_all( "/href='(.*?)'>(.*?)<\/a>/", $yearly_list, $results );
if ( ! empty( $results ) ) {
// $urls に URL を追加
foreach ( $results[1] as $result ) {
$urls[] = $result;
// ページ数を取得
$year = substr( untrailingslashit( $result ), -4 );
$args = array(
'post_type' => 'add-type',
'year' => $year,
);
$the_query = new WP_Query( $args );
$max_num_pages = $the_query->max_num_pages;
// ページ数付きの URL を追加
for ( $i = 2; $i <= $max_num_pages; $i++ ) {
$urls[] = trailingslashit( $result ) . 'page/' . $i . '/';
}
}
}
}
return $urls;
}
add_action(
'init',
function () {
add_filter( 'ShifterURLS::AppendURLtoAll', 'append_archive_urls' );
}
);
アーカイブのパーマリンク構造はそれぞれ異なると思うので格納する URL は適宜調整してください。
元々 https://127.0.0.1:8443/wp-json/shifter/v1/urls?page=1
にアクセスしたときには次のような JSON が返ってきていました。
{
"datetime": "2024-12-17 13:19:02 UTC",
"page": 1,
"start": 0,
"end": 100,
"limit": 100,
"items": [
{
"link_type": "home",
"post_type": "",
"link": "https://127.0.0.1:8443/",
"path": "/"
},
[略]
{
"link_type": "author_link",
"post_type": "",
"link": "https://127.0.0.1:8443/author/admin/",
"path": "/author/admin/"
}
],
"request_type": "TOP",
"request_path": "/",
"count": 29,
"finished": true
}
先のコードを使うと次のような JSON が返ってくるようになります。"link_type": "author_link",
の下に "link_type": "from_filter_hook",
の項目が追加されました。
{
"datetime": "2024-12-17 13:19:02 UTC",
"page": 1,
"start": 0,
"end": 100,
"limit": 100,
"items": [
{
"link_type": "home",
"post_type": "",
"link": "https://127.0.0.1:8443/",
"path": "/"
},
[略]
{
"link_type": "author_link",
"post_type": "",
"link": "https://127.0.0.1:8443/author/admin/",
"path": "/author/admin/"
},
{
"link_type": "from_filter_hook",
"post_type": "",
"link": "https://127.0.0.1:8443/add-type/2024/",
"path": "/add-type/2024/"
},
{
"link_type": "from_filter_hook",
"post_type": "",
"link": "https://127.0.0.1:8443/add-type/2023/",
"path": "/add-type/2023/"
},
{
"link_type": "from_filter_hook",
"post_type": "",
"link": "https://127.0.0.1:8443/add-type/2023/page/2/",
"path": "/add-type/2023/page/2/"
}
],
"request_type": "TOP",
"request_path": "/",
"count": 39,
"finished": true
}
何かもっといい方法がないかなぁと考えてますが、また何か思いついたら追記します。