2015年2月5日木曜日

不動産カスタム投稿メモ

add_action('init', 'my_estate_init');
function my_estate_init()
{
  $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,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => 5,
//  'supports' => array('title','editor','author','thumbnail','excerpt','comments')
    'supports' => array('title','editor')
  );
  register_post_type('estate',$args);
  flush_rewrite_rules( false );
}

/*** カスタムフィールド項目定義 ***
 *
 * $meta_arr[$id] = array($name,$array,$option);
 *   $id:     キー
 *   $name:   項目名
 *   $array:  保存データ形式('array':配列、'single':テキスト)
 *   $option: オプション項目
 *   checkboxの場合は配列として登録される
 */
$meta_arr['esArea'     ] = array('エリア',         'array', array('ビジネス街', '住宅街', '商店街', '歓楽街'));
$meta_arr['esCatchcopy'] = array('キャッチコピー', 'single');
$meta_arr['esProfeel'  ] = array('物件紹介文',     'single');
$meta_arr['esRental'   ] = array('賃料',           'single');
$meta_arr['esNotes'    ] = array('備考',           'single');

/*** カスタムフィールドコンテンツの作り込み ***/
function my_meta_boxes() {

    global $post, $meta_arr;

    //metaの現在の登録値を取得(可変変数)
    foreach($meta_arr as $meta => $meta_val) {
        $true = ( $meta_val[1] == 'single' )? true: false;
        $val = $meta.'Val';
        $nam = $meta.'Nam';
        $$nam = $meta_val[0];
        $$val = get_post_meta( $post->ID, $meta, $true );
    }

?>
<div class="postbox postbox_estate">
    <h4>物件プロフィール</h4>
    <dl>
        <dt><?php echo $esAreaNam ?>:</dt>
        <dd>
        <?php foreach ($meta_arr['esArea'][2] as $optn): ?>
                <label><input type="checkbox" name="esArea[]" value="<?php echo $optn ?>"<?php if ( is_array($esAreaVal[0]) ) {if ( in_array($optn, $esAreaVal[0]) ) echo ' checked="checked"';} ?> /> <?php echo $optn ?></label>
        <?php endforeach ?></dd>
        <dt><?php echo $esCatchcopyNam ?>:</dt>
        <dd><textarea name="esCatchcopy" type="textfield" rows="2" style="width:90%"><?php echo $esCatchcopyVal ?></textarea><br />
            見出しの下に表示されます。一行に収まる長さでご入力ください。</dd>
        <dt><?php echo $esProfeelNam ?>:</dt>
        <dd><textarea name="esProfeel" type="textfield" rows="4" style="width:90%"><?php echo $esProfeelVal ?></textarea></dd>
    </dl>
</div>
<div class="postbox postbox_estate">
    <h4>物件詳細データ</h4>
    <dl>
        <dt><?php echo $esRentalNam ?>:</dt>
        <dd><input name="esRental" type="text" value="<?php echo $esRentalVal ?>" style="width:60%"><br />
        ○○円/月</dd>
        <dt><?php echo $esNotesNam ?>:</dt>
        <dd><textarea name="esNotes" type="textfield" rows="4" style="width:90%"><?php echo $esNotesVal ?></textarea></dd>
    </dl>
</div>
<?
}
/*** カスタムフィールド入力値の保存 ***/
function save_postdata( $post_id ) {

    global $post, $meta_arr;
    foreach($meta_arr as $meta => $arr) {
        $true = ( $arr == 'single' )? true: false;

        $meta_cur = get_post_meta($post_id, $meta, $true);
        $meta_new = $_POST[$meta];

        if( $meta_cur == "" && $meta_new != "") {
            add_post_meta($post_id, $meta, $meta_new, true);
        } elseif ( $meta_cur != $meta_new ) {
            update_post_meta($post_id, $meta, $meta_new);
        } elseif ( $meta_new == "" ) {
            delete_post_meta($post_id, $meta, get_post_meta($post_id, $meta_cur, true));
        }
    }
}
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');

/*** 投稿画面にカスタムフィールドのセクションを追加 ***/
function create_meta_box() {

    if ( function_exists('add_meta_box') )
//  add_meta_box('id', 'title', 'callback', 'page', 'context', 'priority');
        add_meta_box( 'my-meta-boxes', '物件登録カスタムフィールド', 'my_meta_boxes', 'estate', 'normal', 'high' );
}

0 件のコメント:

コメントを投稿