如何限制WordPress的类别选择只是一个?

问题描述 投票:4回答:2

我有一个自定义后类型和自定义分类设置 - 相当标准的东西。

不过,我想知道我可以从每个岗位选择多个分类类别限制我的客户?

我不介意他们能够创建和删除分类类型,但我不想让他们选择不止一个。因为这些都是复选框,就可以。也许单选按钮可能会奏效?

我见过的解决方案使用jQuery来更改这些字段,但他们似乎有点哈克。什么是最好的实践方式做到这一点?

附,是我的自定义分类箱的屏幕截图。

php wordpress wordpress-theming
2个回答
0
投票

我知道这不是一个完全成熟的答案,但作为一个下拉会做的伎俩上市的类别(或分类术语)这样简单的事情。我不知道一个个人的,但有一定是插件,做到这一点。不要介意,不过这肯定给我的印象是一直前解决的问题。


0
投票

据我所知,我们要使用jQuery。这是“哈克”,但工作很容易和纯PHP / WP的解决方案是非常复杂的,如果我没记错的好。 这是必要的代码:

jQuery("#categorychecklist input").each(function(){
    this.type = 'radio';
});

我们必须要在页面上/wp-admin/post.php/wp-admin/post-new.php运行代码。它应该负荷运行,如果创建了一个新的类别,然后再次运行。为了解决新类别的问题,我使用的MutationObserver

foreach( array( 'post', 'post-new' ) as $hook )
    add_action( "admin_footer-$hook.php", 'convert_cats_to_radio' );

function convert_cats_to_radio(){
    global $post;
    if( $post->post_type !== 'post') // select your desired Post Type
        return;

    ?>
    <script type="text/javascript">
    function makeRadioButtons(){
        jQuery("#categorychecklist input").each(function(){
            this.type = 'radio';
        });
    }
    function newCategoryObserver(){
        // Example from developer.mozilla.org/en-US/docs/Web/API/MutationObserver
        var targetNode = document.getElementById('categorychecklist');
        var config = { attributes: true, childList: true, subtree: true };
        var callback = function(mutationsList) {
            for(var mutation of mutationsList) {
                if (mutation.type == 'childList') {
                    makeRadioButtons();
                }
            }
        };
        var observer = new MutationObserver(callback);
        observer.observe(targetNode, config);
    }
    newCategoryObserver();
    makeRadioButtons();
    </script>
    <?php
}

相关:this old plugin仍然运转完好解决的子类层次结构的问题。默认情况下,WP,当选择一个子类不其父类别下,但在列表的顶部显示。这也是一个jQuery的解决方案,并写了铅WP核心开发者的时间。

© www.soinside.com 2019 - 2024. All rights reserved.