Blog

【WordPress】マルチサイトでカスタム投稿を一括追加する方法

2019/01/26

マルチサイト

備忘録です。

現在、マルチブログサイトを制作していますが、各サイトでCustom Post Type UIを使ってカスタム投稿を作るのは面倒だなと思い調べたところ、下記サイトにてプラグインを使わないカスタム方法の記述がありました。

参考サイト
マルチサイトでサイト毎にカスタム投稿タイプを使う

custom-post-type.phpファイルを作成して記述

<?php
    add_action('init', 'create_post_type');
    function create_post_type(){
      // カスタム投稿タイプ:フロントエンドエンジニアのblog
      $labels = array(
        'name' => _x('フロントエンドエンジニアのblog', 'post type general name'),
        'singular_name' => _x('フロントエンドエンジニアのblog', 'post type singular name'),
        'menu_name' => _x('エンジニアblog', 'admin menu'),
        'name_admin_bar' => _x('エンジニアblog', 'add new on admin bar'),
        'add_new' => _x('新規追加','blog'),
        'add_new_item' => __('新規blog記事を追加'),
        'new_item' => __('新規blog記事'),
        'edit_item' => __('blog記事の編集'),
        'view_item' => __('blog記事を表示'),
        'all_items' => __('blog記事一覧'),
        'search_items' => __('blog記事を検索'),
        'parent_item_colon' => '',
        'not_found' => __('blog記事が見つかりませんでした。'),
        'not_found_in_trash' => __('ゴミ箱内にblog記事が見つかりませんでした。'),
      );
      $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'exclude_from_search' => false,
        'query_var' => true,
        'rewrite' => array('slug' => '', 'with_front' => false),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => 20,
        'supports' => array('title', 'editor', 'thumbnail', 'author', 'trackbacks', 'comments', 'revisions'),
      );
      register_post_type('blog', $args);
     
      //カスタムタクソノミー:blogカテゴリー
      $labels = array(
        'name' => _x( 'カテゴリー', 'taxonomy general name' ),
        'singular_name' => _x( 'カテゴリー', 'taxonomy singular name' ),
        'add_new_item' => __( '新規カテゴリーを追加' ),
        'edit_item' => __( 'カテゴリーの編集' ),
        'update_item' => __( 'カテゴリーを更新' ),
        'search_items' => __( 'カテゴリーを検索' ),
      );
      $args = array(
        'labels' => $labels,
        'public' => true,
        'show_ui' => true,
        'show_admin_column' => true,
        'rewrite' => array('slug' => 'blog', 'with_front' => false),
        'hierarchical' => true,
        'update_count_callback' => '_update_post_term_count',
      );
      register_taxonomy('blog_category', 'blog', $args);
    }
?>

custom-post-type.phpは、テーマディレクトリにincフォルダーを作成してアップしてください。

functions.phpに記述

global $current_blog;
$blog_id = $current_blog->blog_id;
if ($blog_id == 1 ){
  include( TEMPLATEPATH. '/inc/custom-post-type-a.php' );
} elseif ($blog_id == 2 ){
  include( TEMPLATEPATH. '/inc/custom-post-type-b.php' );
} elseif ($blog_id == 3 ){
  include( TEMPLATEPATH. '/inc/custom-post-type-c.php' );
} elseif ($blog_id == 4 ){
  include( TEMPLATEPATH. '/inc/custom-post-type-d.php' );
}

参考サイトの例では、ブログごとにテンプレートを分けた場合を想定しています。
同じテンプレートならcustom-post-type.phpを指定すれば大丈夫です。

カテゴリー

月間アーカイブ

MORE

ミュージシャンズ・プラザ

神社仏閣ホームーページ制作

ホームページ制作問合せ