How can I limit WordPress category selection to just one?

China☆狼群 提交于 2019-12-10 14:24:46

问题


I have a custom post type and a custom taxonomy setup - pretty standard stuff.

However, I would like to know how I can restrict my client from selecting more than one taxonomy category per post?

I don't mind them being able to create and delete taxonomy types, but I don't want them selecting more than one. Because these are checkboxes, they CAN. Perhaps radio buttons might work?

I have seen solutions use jQuery to change these fields, but they seem a bit hacky. What's the best practice way to do this?

Attached, is a screenshot of my custom taxonomy box.


回答1:


I realize this isn't a full blown answer but something as simple as listing the categories (or taxonomy terms) with a dropdown would do the trick. I don't know of one personally but there's got to be plugin that does this. No offense but this certainly strikes me as a problem that's been solved before.




回答2:


As far as I know, we have to use jQuery. It's "hacky" but works easily and a pure PHP/WP solution is really complex if my memory serves me well.
This is the necessary code:

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

We have to run the code on the pages /wp-admin/post.php and /wp-admin/post-new.php. It should run on load and then run again if a new category is created. To solve the new category issue I used a 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
}

Relevant: this old plugin still works perfectly to solve the issue of sub-categories hierarchy. By default in WP, when one sub-category is selected it doesn't show under its parent category but on top of the list. It's also a jQuery solution and written by a lead WP core developer at the time.



来源:https://stackoverflow.com/questions/11450327/how-can-i-limit-wordpress-category-selection-to-just-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!