真的,你可能会对它进行更多的改进,并想出更好的名称,我只是想让它功能化^ ^ ^!
首先->让我们包括JQuery,因为我不想在vanilla JS中这样做。
function lets_include_jquery() {
wp_enqueue_script( \'jquery\' );
}
add_action( \'admin_enqueue_scripts\', \'lets_include_jquery\' );
现在,让我们在管理面板的页脚中添加一个脚本来处理有趣的内容。
function lets_add_our_js() {
if(get_current_post_type() == \'POST_TYPE_HERE\') : ?>
<script type="text/javascript">
jQuery(document).ready(function($){
$(\'#TAXONOMY_NAME_HEREchecklist li label > input:checkbox:not(:checked)\')
.parent(\'label\')
.next(\'ul.children\')
.css(\'display\', \'none\');
// The following is 100% optional since our children are hidden, I like it though.
$(\'#TAXONOMY_NAME_HEREchecklist li label > input:checkbox:not(:checked)\')
.parent(\'label\')
.next(\'ul.children\')
.find(\'input:checkbox\')
.attr(\'disabled\', \'disabled\');
// This is required
$(\'#TAXONOMY_NAME_HEREchecklist li label > input:checkbox\').click(function(){
if($(this).is(\':checked\')){
$(this).parent(\'label\').next(\'.children\').css(\'display\', \'block\');
$(this).parent(\'label\').next(\'.children\').find(\'input:checkbox\').removeAttr(\'disabled\'); // This line is really optional, I like it though.
}
else{
$(this).parent(\'label\').next(\'.children\').css(\'display\', \'none\');
$(this).parent(\'label\').next(\'.children\').find(\'input:checkbox\').attr(\'disabled\', \'disabled\'); // This line is really optional, I like it though.
}
});
});
</script>
<?php endif;
}
add_action(\'admin_footer\', \'lets_add_our_js\');
这是一个相当通用的脚本,没有什么特别之处。接下来,我们添加
neat little function I found online!
function get_current_post_type() {
global $post, $typenow, $current_screen;
//we have a post so we can just get the post type from that
if ( $post && $post->post_type )
return $post->post_type;
//check the global $typenow - set in admin.php
elseif( $typenow )
return $typenow;
//check the global $current_screen object - set in sceen.php
elseif( $current_screen && $current_screen->post_type )
return $current_screen->post_type;
//lastly check the post_type querystring
elseif( isset( $_REQUEST[\'post_type\'] ) )
return sanitize_key( $_REQUEST[\'post_type\'] );
//we do not know the post type!
return null;
}
呼!现在
there are a few things you need to change... 无论你在哪里找到
POST_TYPE_HERE
您需要将其替换为任何帖子类型,以便脚本只显示在那些管理页面上。如果有多个,则为
in_array()
. 无论你看到哪里
TAXONOMY_NAME_HERE
您需要将其替换为metabox所拥有的分类名称。因此,如果我创建一个名为“tax\\u news”的分类法/类别,我的Jquery将显示:
\'#tax_newschecklist\'
等
It\'s Up To You 重命名和注释此代码,以便对您和您的场景有意义,以供将来使用!我强烈建议你这样做!