wp admin - Limit post top level categories to one

admin2025-06-05  1

My website have many posts, ~9 main categories and "unlimited" subcategories.

I'd like to force in WordPress panel that you can't choose more than 1 top level category but you can choose unlimited subcategories of this main one.

One plugin changed checkboxes into radiobuttons and it works great but only for one level and I need more depth.

Is there any way to do that? Thanks in advance.

My website have many posts, ~9 main categories and "unlimited" subcategories.

I'd like to force in WordPress panel that you can't choose more than 1 top level category but you can choose unlimited subcategories of this main one.

One plugin changed checkboxes into radiobuttons and it works great but only for one level and I need more depth.

Is there any way to do that? Thanks in advance.

Share Improve this question asked Aug 2, 2018 at 8:41 HazeHybridHazeHybrid 134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Late answer but I figured I'd throw in my idea since I had a similar problem recently.

Basically I did this by injecting a script into the editor views. My function looked something like this:

(function() {
    var o=function() {
    //add a click handler to category checkboxes
    jQuery("#categorychecklist input").click(function() {
        //get top-level category (in case child is checked)
        var tlc = jQuery("#category-all>ul>li").has(this);
        //if top-level category is not checked, check it
        jQuery(">label>input",tlc).prop("checked", true);
        //uncheck all boxes from other top-level categories
        jQuery("#category-all>ul>li").not(tlc).find("input:checked").prop("checked", false);
    });}
    //run this function onload
    var n=window.onload;"function"!=typeof window.onload?window.onload=o:window.onload=function(){n&&n(),o()};
})();

I hope this helps somebody. To add a script just use something like this: How to enqueue scripts on custom post add/edit pages?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749121789a316548.html

最新回复(0)