php - 如果选择了特定类别,则在管理帖子页面上显示一些内容

标签 php jquery wordpress

我试图让 WordPress 的管理帖子页面在选择特定类别(例如“精选”)时显示某种形式的输出。

类似这样的内容(原始页面):

original-image

当我选择“精选”类别(选定的“精选”类别)时,我想要显示一些内容,如下所示:

when-selected

意味着页面会检查是否选中了特定类别,如果是,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/

相关文章:

javascript - 由 jquery 函数填充的文本框需要小数

php - Wordpress 网站中的 fatal error : Class 'DOMDocument' not found,

linux - Wordpress 本地主机插件安装

javascript - 现在使用 WordPress 中的 GM api 在谷歌地图上显示标记

php - 如何清除hbase中的表?

java - Java 中的 Drupal 风格 base64 编码

php - 将 mySQL 结果拆分为两个 <div> 并限制为 4

JavaScript 无法在 XAMPP 中运行

javascript - 在javascript中将Json日期转换为 "Date"

PHP 邮件清理技术