問題描述

我有一個 CPT 站點 (自定義帖子類型的縮寫)”bagp_deals” 和自定義分類”ba_locations” 和”ba_cats” 基本上它的”Deals” 的”Location” 和”Categories” 的分類分類。在默認編輯屏幕上,我想將選擇限制為每個 (一個位置和一個類別) 中的一個,我正在嘗試使用 JQuery 進行此操作,我注意到 ba_locations 的字段自定義分類法命名為”tax_input[ba_locations][]”,到目前為止有這個代碼:

jQuery("input[name=tax_input[ba_locations][]]").click(function () {
    selected = jQuery("input[name=tax_input[ba_locations][]]").filter(":checked").length;
    if (selected > 1){
        jQuery("input[name=tax_input[ba_locations][]]").each(function () {
                jQuery(this).attr("checked", false);
        });
        jQuery(this).attr("checked", true);
    }
});

假設巫婆將複選框選擇限制為一個。由於某種原因,我無法讓它上​​班。

問題

所以問題是為什麼這不工作?或者你有更好的解決方案來限制選擇只有一個?

任何幫助都不勝感激。

更新:

這是我使用的工作代碼:

jQuery("input[name="tax_input[ba_locations][]"]").click(function () {
    selected = jQuery("input[name="tax_input[ba_locations][]"]").filter(":checked").length;
    if (selected > 1){
        jQuery("input[name="tax_input[ba_locations][]"]").each(function () {
                jQuery(this).attr("checked", false);
        });
        jQuery(this).attr("checked", true);
    }
});

最佳解決方案

而不是使用 jQuery 進行黑客攻擊,而更可靠的解決方案是用 PHP 自己替換元框。

無論如何,問題很可能是選擇器中的'[‘ 和’]’ 字符:

"input[name=tax_input[ba_locations][]]"

可以重寫為

"input[name=tax_input\[ba_locations\]\[\]]"

https://stackoverflow.com/questions/2786538/need-to-escape-a-special-character-in-a-jquery-selector-string

次佳解決方案

我有一個 php 解決方案給你:

add_filter('wp_terms_checklist_args', 'htmlandcms_select_one_category');
function htmlandcms_select_one_category($args) {
    if (isset($args['taxonomy']) && $args['taxonomy'] == 'category_portfolio') {
        $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.']';

        /** @var $popular_cats */
        $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
        /** @var $selected_cats */
        $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";
    }
}

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。