我试图让 WordPress 的管理帖子页面在选择特定类别(例如“精选”)时显示某种形式的输出。
类似这样的内容(原始页面):
当我选择“精选”类别(选定的“精选”类别)时,我想要显示一些内容,如下所示:
意味着页面会检查是否选中了特定类别,如果是,Wordpress 会生成一个选项菜单供用户填写。
我该如何去做呢?我正在查看 Wordpress Codex,但到目前为止,还没有任何结果。
最佳答案
您必须仅在该屏幕中打印一些 jQuery,然后就需要监听实时更改并进行显示/隐藏操作。
以下内容来自this WPSE Answer 。您必须调整元素 ID。另外,jQuery 的 live
方法是 deprecated ,也改变它。
add_action('admin_head', 'wpse_53486_script_enqueuer');
function wpse_53486_script_enqueuer() {
global $current_screen;
if ( 'post' == $current_screen->id )
{
echo <<<HTML
<script type="text/javascript">
jQuery(document).ready( function($) {
if ( $('#in-category-6').is(':checked') ) {
$("form#adv-settings label[for='myplugin_sectionid-hide']").show();
$('#myplugin_sectionid').show();
} else {
$('#myplugin_sectionid').hide();
$("form#adv-settings label[for='myplugin_sectionid-hide']").hide();
}
$('#in-category-6').live('change', function(){
if ( $(this).is(':checked') ) {
$('#myplugin_sectionid').show();
$("form#adv-settings label[for='myplugin_sectionid-hide']").show();
} else {
$('#myplugin_sectionid').hide();
$("form#adv-settings label[for='myplugin_sectionid-hide']").hide();
}
});
});
</script>
HTML;
}
}
另一种打印 targeted admin pages 中脚本的方法.
关于php - 如果选择了特定类别,则在管理帖子页面上显示一些内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20452546/