Help us understand the problem. What is going on with this article?

Wordpressのカスタム投稿に必要な記述

カスタム投稿タイプ:キャンペーン(campaign)
カスタムフィールド名:ジャンル(genre-taxonomy)

/**
 *  カスタム投稿:キャンペーン追加
 */
function campaign_post_type()
{
    register_post_type('campaign', [ // 投稿タイプ名の定義
        'labels' => [
            'name'          => 'キャンペーン', // 管理画面上で表示する投稿タイプ名
            'singular_name' => 'キャンペーン記事',    // カスタム投稿の識別名
            'all_items' => 'キャンペーン一覧',
            'featured_image' => 'サムネイル画像(推奨: 350px : 150px)',
            'set_featured_image' => 'サムネイル画像を設定',
            'remove_featured_image' => 'サムネイル画像を削除',
            'use_featured_image' => 'サムネイル画像として使用'
        ],
        'public'        => true,  // 投稿タイプをpublicにするか
        'has_archive'   => true, // アーカイブ機能ON/OFF
        'menu_position' => 5,     // 管理画面で表示される位置。5は投稿の下、デフォルトは25(コメントの下)
        'show_in_rest'  => true,  // 5系から出てきた新エディタ「Gutenberg」を有効にする
        'parent'  => 'main',
        'supports' => array('title','editor','thumbnail')
    ]);
}
add_action('init', 'campaign_post_type');

/**
 *  カスタム投稿:投稿数設定
 */
function campaign_posts_count($query)
{
    if (is_admin() || ! $query->is_main_query()) {
        return;
    }
    if ($query->is_archive('campaign')) { //カスタム投稿タイプを指定
        $query->set('posts_per_page', '6'); //表示件数を指定
    }
}
add_action('pre_get_posts', 'campaign_posts_count');


/**
 *  カスタム投稿:検索後ページ設定
 */
function custom_search_campaign_template($template){
    if ( is_search() && is_post_type_archive('campaign') ){
        $post_types = get_query_var('campaign');
        foreach ( (array) $post_types as $post_type )
            $templates[] = "search-campaign.php";
        $templates[] = 'search.php';
        $template = get_query_template('search',$templates);
    }
    return $template;
}
add_filter('template_include','custom_search_campaign_template');


/**
 *  カスタム投稿:タクソノミー設定
 */
function genre_taxonomy() {
    register_taxonomy(
    'genre-taxonomy', //タクソノミー名
    'campaign', //利用するカスタム投稿
    array(
        'label' => 'ジャンル',
        'singular_label' => 'ジャンル記事',
        'labels' => array(
            'add_new_item' => 'ジャンルを追加'
        ),
        'public' => true,
        'show_ui' => true,
        'show_admin_column' => true,
        'show_in_nav_menus' => true,
        'hierarchical' => true,
        'show_in_rest'  => true, // 5系から出てきた新エディタ「Gutenberg」を有効にする
        )
    );
}
add_action( 'init', 'genre_taxonomy' );
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした