Created
March 20, 2017 08:03
-
-
Save nyankichi-n-n/422592428664f8942d868be7dee7816e to your computer and use it in GitHub Desktop.
[デモ会員サイト構築]8. カスタム投稿にて、カテゴリーを指定しなかった場合に、初期値を設定
This file contains 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
<?php | |
// 一行目はシンタックスハイライトするために追加しています。 | |
//****** 自前のグローバル変数 ****** | |
$my_travel_category_default = '旅行全般'; | |
$my_cake_category_default = 'ケーキ全般'; | |
// カスタム投稿タイプでカテゴリ未選択時にデフォルトを設定 | |
function add_defaultcategory_automatically_travel($post_ID) { | |
global $wpdb; | |
// 設定されているカスタム分類のタームを取得 | |
$curTerm = wp_get_object_terms($post_ID, 'travel_category'); | |
// 既存のターム指定数が 0(つまり未設定)であれば)初期値のターム名を指定 | |
if (0 == count($curTerm)) { | |
// 初期値のターム名からターム ID を取得して設定する | |
global $my_travel_category_default; | |
$term = get_term_by('name',$my_travel_category_default , 'travel_category'); | |
if ( !empty( $term ) ) { | |
$defaultTerm = array( $term->term_id ); | |
wp_set_object_terms($post_ID, $defaultTerm, 'travel_category'); | |
} | |
} | |
} | |
add_action('publish_travel', 'add_defaultcategory_automatically_travel'); | |
function add_defaultcategory_automatically_cake($post_ID) { | |
global $wpdb; | |
// 設定されているカスタム分類のタームを取得 | |
$curTerm = wp_get_object_terms($post_ID, 'cake_category'); | |
// 既存のターム指定数が 0(つまり未設定)であれば)初期値のターム名を指定 | |
if (0 == count($curTerm)) { | |
// 初期値のターム名からターム ID を取得して設定する | |
global $my_cake_category_default; | |
$term = get_term_by('name',$my_cake_category_default , 'cake_category'); | |
if ( !empty( $term ) ) { | |
$defaultTerm = array( $term->term_id ); | |
wp_set_object_terms($post_ID, $defaultTerm, 'cake_category'); | |
} | |
} | |
} | |
add_action('publish_cake', 'add_defaultcategory_automatically_cake'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment