0

I'm in need of a solution that allows me to restrict the user/author to only use maximum of two categories per new post. One post can only have one or two categories.

The only similar solution that I have found is when you make the categories as radio buttons instead of check boxes. But that will restrict the user to choose only one category. Only one category per post

Any idea have to solve this?

2
  • Do you have any idea what to do when the user chooses too many categories? Let's assume that JavaScript is turned off by the user and he selects 5 instead of 2 categories. What exactly would you do? Remove all cats? Only 3? If so: Which ones? What would you tell the user and where?
    – kaiser
    Commented Feb 9, 2015 at 21:38
  • I believe (s)he wants a JavaScript snippet that counts the number of selected cats/tags and alerts the users they can't check the third when they try to, so the user has to choose the 2 cats/tags. Unfortunately, this is not a snippet shop. It's a place for learning. We can help you spot the bugs in your code @Flumra. Now, where exactly is your code? [ And I also believe (s)he never thought users can turn javascript off. :) ]
    – tao
    Commented Feb 10, 2015 at 2:12

1 Answer 1

1

Thanks the the replies. I solved my problem with jQuery.

Here is the code:

jQuery(document).ready(function ($) {
$("#category-tabs li.hide-if-no-js").hide(); //Hides Most Used tab

$("#category-all input:checkbox").change(function () {
    var max = 2; // Max allowed cats
    var count = $("#category-all input:checked").length; //counts selected cats
    if (count > max) {
        $(this).prop("checked", "");
        alert('You are only allowed to select ' + max + ' categories.'); //alert message when user tries to select a third cat
    }
  });

});
3
  • 1
    which is only a solution for the post edit page, but will not solve the problem for quick edit or editing via the smartphone apps (in other words changes done via xml-rpc). Commented Feb 11, 2015 at 11:18
  • Quick edit can also be solved with jQuery and thanks for the tip on the xml-rpc related problems.
    – Flumra
    Commented Feb 12, 2015 at 13:13
  • Nice but is there any option to restrict to use not more than 3 tag
    – Firefog
    Commented Nov 23, 2015 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.