前提・実現したいこと
WordPressの投稿画面の親カテゴリーを選択出来ないようにしたくて、
下記コードで通常の投稿は実装できるのですが、カスタム投稿タイプにも反映さたい場合、どのように変更したら良いか解らず質問いたしました。
該当のソースコード
PHP
1require_once(ABSPATH . '/wp-admin/includes/template.php'); 2class Danda_Category_Checklist extends Walker_Category_Checklist { 3 4 function start_el( &$output, $category, $depth, $args, $id = 0 ) { 5 extract($args); 6 if ( empty($taxonomy) ) 7 $taxonomy = 'category'; 8 9 if ( $taxonomy == 'category' ) 10 $name = 'post_category'; 11 else 12 $name = 'tax_input['.$taxonomy.']'; 13 14 $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : ''; 15 $cat_child = get_category_children($category->term_id); 16 if($cat_child !== "") { 17 $output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), true, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>'; 18 }else{ 19 $output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>'; 20 } 21 } 22 23} 24 25function lig_wp_category_terms_checklist_no_top( $args, $post_id = null ) { 26 $args['checked_ontop'] = false; 27 $args['walker'] = new Danda_Category_Checklist(); 28 return $args; 29} 30add_action( 'wp_terms_checklist_args', 'lig_wp_category_terms_checklist_no_top' );
試したこと
PHP
1 $cat_child = get_category_children($category->term_id); 2 if($cat_child !== "")
上記の条件が投稿のみにしか動作していないようなので、他に変えてみたのですがうまくいきませんでした。
また、ここの条件は子カテゴリを持つ親カテゴリ全てを対象としたいです。
プラグインを使用する以外の方法がありましたら教えていただきたいです。
どうぞよろしくお願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/18 22:31