MENU

【Swell】カスタム投稿タイプの月別アーカイブを表示する方法

やりたいこと

ブログテーマ「swell」を使用して、カスタム投稿タイプのサイドバーに月別アーカイブの表示をしたい。

使用したプラグイン

カスタム投稿タイプ「Custom Post Type UI
パーマリンク「Custom Post Type Permalinks
ウィジェット「Custom Post Type Widgets

問題点

プラグイン「Custom Post Type Permalinks」と「Custom Post Type Widgets」の相性が悪いのか、リンク先に飛ぶとページが表示されない。

解決策

Custom Post Type Widgets」は使用せず、フックを使う。

子テーマのfunction.phpに以下を追記する。
「blog」の部分は設定したスラッグに変更する。(2箇所)

/**
 * ウィジェット「アーカイブ」にフィルター
 * require Custom Post Type Permalinks
 */

add_filter( 'widget_archives_args', 'my_widget_archives_args', 10, 1);
add_filter( 'widget_archives_dropdown_args', 'my_widget_archives_args', 10, 1);
function my_widget_archives_args( $args ){
	if ( ! is_admin() ) {
		if ( 'blog' == get_post_type() ) {
			$args['post_type'] = 'blog';
		}
	}
	return $args;
}

参考:https://nldot.info/steps-to-deploy-an-archive-widget-for-a-custom-post-type/

一日探してやっと辿り着いた情報なので、忘れた時用にメモしました🙏

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次
閉じる