1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

WordPressでカテゴリーのアーカイブにカスタム投稿の記事も表示させる

Posted at

ワードプレスのテーマtwentytwentyを基盤に、カテゴリーのアーカイブページにカスタム投稿の記事も一覧表示させる実装を行ったので、備忘録的にこの記事を残す。

環境情報

PHP:version 7.3.12
WordPress:version 5.5.3
WPテーマ:twentytwenty

作業

通常はポストタイプ名が post の投稿のみ表示される仕様のようだ。

functions.php
// カテゴリーのアーカイブにカスタム投稿の記事も表示させる
function add_customtype_archive( $wp_query ) {
    if ( is_admin() || ! $wp_query->is_main_query() )
        return;

    if ( $wp_query->is_category() || $wp_query->is_tag() ) {
        $wp_query->set( 'post_type', array( 'post', 'カスタム投稿タイプ' ));
        return;
    }
}
add_action( 'pre_get_posts', 'add_customtype_archive' );

「カスタム投稿タイプ」の部分を表示させたい投稿のポストタイプ名に変更してお使いください。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?