-
-
Save naokazuterada/5556068 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ========================================================== | |
// | |
// カスタムポストタイプ | |
// src: https://gist.github.com/naokazuterada/5556068 | |
// | |
// 〜 How to Use 〜 | |
// 1.これをそのまま、function.phpにコピー&ペースト | |
// 2.これ以降の"example"を(SublimeTextなら⌘Dで)全選択し、任意の名前に全置換 | |
// 3.[A]のラベルを編集 | |
// 4.カスタムフィールド | |
// 設定しない場合 | |
// - [B]の行を消去 | |
// - [C]の行以降を消去 | |
// 設定する場合 | |
// - [C]の行以降の"field1"や"field2"を任意の名前に置換するなど各種設定 | |
// 5.分類(「投稿」における「カテゴリー」にあたるもの) | |
// 設定する場合 | |
// - [D]のラベル名を任意に設定 | |
// 設定しない場合 | |
// - [E]の関数register_taxonomyを消去 | |
// ------------------------ | |
// CustomPostType: example | |
// ------------------------ | |
add_action('init', 'addCPT_example'); | |
function addCPT_example(){ | |
$cpt_name = 'example'; | |
// [A]ラベル | |
$labels = array( | |
'name' => 'サンプル', | |
'singular_name' => 'サンプル', | |
'add_new' => 'サンプルを追加', | |
'add_new_item' => '新しいサンプルを追加', | |
'edit_item' => 'サンプルを編集', | |
'new_item' => '新しいサンプル', | |
'view_item' => 'サンプルを表示', | |
'search_items' => 'サンプルを探す', | |
'not_found' => 'サンプルはありません', | |
'not_found_in_trash' => 'ゴミ箱にサンプルはありません', | |
'parent_item_colon' => '' | |
); | |
$args = array( | |
'labels' => $labels, | |
'public' => true, | |
'capability_type' => 'post', | |
'hierarchical' => false, | |
'has_archive' => true, | |
'supports' => array( | |
'title', | |
'editor' | |
), | |
'register_meta_box_cb' => 'example_meta_box' // [B]カスタムフィールド有効化 | |
); | |
register_post_type($cpt_name, $args); | |
// [E]分類 | |
register_taxonomy( | |
$cpt_name.'-type', // [D] | |
$cpt_name, | |
array( | |
'label' => 'タイプ', // [D] | |
'show_ui' => true, | |
'hierarchical' => true | |
) | |
); | |
} | |
// [C]カスタムフィールド | |
function example_meta_box($post){ | |
add_meta_box( | |
'example_meta', | |
'設定欄', | |
'example_meta_callback', | |
'example', | |
'normal', | |
'high' | |
); | |
} | |
function example_meta_callback($post, $box){ | |
// カスタムフィールドの値を取得 | |
$field1 = get_post_meta($post->ID, 'field1', true); | |
$field2 = get_post_meta($post->ID, 'field2', true); | |
// | |
echo wp_nonce_field('example_meta', 'my_meta_nonce'); | |
// 入力域 | |
echo 'field1:<input type="text" name="field1" value="' . $field1 . '" /><br />'; | |
echo 'field2:<input type="text" name="field2" value="' . $field2 . '" />'; | |
} | |
// 保存処理 | |
add_action('save_post', 'example_meta_update'); | |
function example_meta_update($post_id){ | |
if (!wp_verify_nonce( $_POST['my_meta_nonce'], 'example_meta')) { | |
return $post_id; | |
} | |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { | |
return $post_id; | |
} | |
if ('example' == $_POST['post_type']) { | |
if(!current_user_can('edit_post', $post_id)) { | |
return $post_id; | |
} | |
} else { | |
return $post_id; | |
} | |
$field1 = $_POST['field1']; | |
$field2 = $_POST['field2']; | |
if($field1 == '') { | |
delete_post_meta($post_id, 'field1'); | |
} else { | |
update_post_meta($post_id, 'field1', $field1); | |
} | |
if($field2 == '') { | |
delete_post_meta($post_id, 'field2'); | |
} else { | |
update_post_meta($post_id, 'field2', $field2); | |
} | |
} | |
// ---------------------------------------------------------- | |
// カスタムポストのパーマリンク「/taxonomy/」を削除 | |
// /post_type/post_taxonomy/post_name -> /post_type/post_name | |
function my_custom_post_type_permalinks_set($termlink, $term, $taxonomy){ | |
return str_replace('/'.$taxonomy.'/', '/', $termlink); | |
} | |
add_filter('term_link', 'my_custom_post_type_permalinks_set',11,3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment