I’ve got a bit of code that limits my custom taxonomy to only allow the user to select one category, by turning the checkboxes in to radio buttons. See the screenshot below for example:

I need the Most Used section to also display as radio buttons, but the code I have only affects the All Categories field. See the below screenshot:

So, how can I extend this code to also cover teh Most Used section?

// limit number of categories a product can have
add_filter('wp_terms_checklist_args', 'htmlandcms_select_one_category');
function htmlandcms_select_one_category($args) {
    if (isset($args["taxonomy"]) && $args["taxonomy"] == "product-category") {
        $args["walker"] = new Walker_Category_Radios;
        $args["checked_ontop"] = false;
    }
    return $args;
}
class Walker_Category_Radios extends Walker {
    var $tree_type = "category";
    var $db_fields = array ("parent" => "parent", "id" => "term_id");
    function start_lvl(&$output, $depth = 0, $args = array()) {
        $indent = str_repeat("t", $depth);
        $output .= "$indent<ul class="children">n";
    }
    function end_lvl(&$output, $depth = 0, $args = array()) {
        $indent = str_repeat("t", $depth);
        $output .= "$indent</ul>n";
    }
    function start_el(&$output, $category, $depth, $args, $id = 0) {
        extract($args);
        if (empty($taxonomy)) {
            $taxonomy = "category";
        }
        if ($taxonomy == "category") {
            $name = "post_category";
        } else {
            $name = "tax_input[" . $taxonomy . "]";
        }
        $class = in_array($category->term_id, $popular_cats) ? " class="popular-category"" : "";
        $output .= "n<li id="{$taxonomy}-{$category->term_id}"$class>" . "<label class="selectit"><input value="" . $category->term_id . "" type="radio" name="" . $name . "[]" id="in-" . $taxonomy . "-" . $category->term_id . """ . checked(in_array($category->term_id, $selected_cats), TRUE, FALSE) . disabled(empty($args["disabled"]), FALSE, FALSE) . " /> " . esc_html( apply_filters("the_category", $category->name)) . "</label>";
    }
    function end_el(&$output, $category, $depth = 0, $args = array()) {
        $output .= "</li>n";
    }
}

Original code taken from this answer.

Read more here: Limit to One Custom Taxonomy Selection, Including Most Used


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , ,

Wordpress related questions and answers: