后端中的分类下拉Metabox

时间:2011-05-28 作者:Kovas

我创建了名为Brands 并将其分级,以便我可以在其中添加汽车品牌和车型,并保持它们之间的关系,如下所示:

福特(Ford)野马(Mustang)蒙迪欧(Mondeo)的问题是,这个列表可能很长,每个帖子只需要一个品牌和一个车型,所以复选框会产生误导。

我正在考虑将metabox拆分为两个(一个用于品牌,一个用于模型),并将其下拉列表。因此,当在第一个下拉列表中选择品牌时,第二个下拉列表将仅显示与该品牌相关的型号。但我不知道如何编码。也许有人能给我举个例子?

1 个回复
最合适的回答,由SO网友:Hameedullah Khan 整理而成

这里有一个例子。我还创建了Gist 使用更通用的代码。

add_action(\'add_meta_boxes\', \'my_custom_metabox\');
function my_custom_metabox() {
    add_meta_box(\'custom-taxonomy-dropdown\',\'Brands\',\'taxonomy_dropdowns_box\',\'post\',\'side\',\'high\');
}

function taxonomy_dropdowns_box( $post ) {
    wp_nonce_field(\'custom-dropdown\', \'dropdown-nonce\');
    $terms = get_terms( \'brands\', \'hide_empty=0\');
    $object_terms = wp_get_object_terms( $post->ID, \'brands\', array(\'fields\'=>\'ids\'));

    // you can move the below java script to admin_head
?>
    <script type="text/javascript">
        jQuery(document).ready(function() {
                jQuery(\'#custombrandoptions\').change(function() {
                    var custombrand = jQuery(\'#custombrandoptions\').val();
                    if ( custombrand == \'0\') {
                        jQuery(\'#custommodeloptions\').html(\'\');
                            jQuery(\'#modelcontainer\').css(\'display\', \'none\');
                    } else {
                        var data = {
                            \'action\':\'get_brand_models\',
                            \'custombrand\':custombrand,
                            \'dropdown-nonce\': jQuery(\'#dropdown-nonce\').val()
                        };
                        jQuery.post(ajaxurl, data, function(response){
                            jQuery(\'#custommodeloptions\').html(response);
                            jQuery(\'#modelcontainer\').css(\'display\', \'inline\');
                        });
                    }
                });
        });
    </script>
    <?php
    echo "Brand:";
    echo "<select id=\'custombrandoptions\' name=\'custombrands[]\'>";
    echo "<option value=\'0\'>None</option>";
    foreach ( $terms as $term ) {
        if ( $term->parent == 0) {
            if ( in_array($term->term_id, $object_terms) ) {
                $parent_id = $term->term_id;
                echo "<option value=\'{$term->term_id}\' selected=\'selected\'>{$term->name}</option>";
            } else {
                echo "<option value=\'{$term->term_id}\'>{$term->name}</option>";
            }
        }
    }
    echo "</select><br />";
    echo "<div id=\'modelcontainer\'";
    if ( !isset( $parent_id)) echo " style=\'display: none;\'";
    echo ">";
    echo "Models:";
    echo "<select id=\'custommodeloptions\' name=\'custombrands[]\'>";
    if ( isset( $parent_id)) {
        $models = get_terms( \'brands\', \'hide_empty=0&child_of=\'.$parent_id);
        foreach ( $models as $model ) {
             if ( in_array($model->term_id, $object_terms) ) {
                echo "<option value=\'{$model->term_id}\' selected=\'selected\'>{$model->name}</option>";
            } else {
                echo "<option value=\'{$model->term_id}\'>{$model->name}</option>";
            }
        }
    }
    echo "</select>";
    echo "</div>";
}

add_action(\'save_post\',\'save_my_custom_taxonomy\');
function save_my_custom_taxonomy( $post_id ) {
    if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
        return;

    if ( !wp_verify_nonce($_POST[\'dropdown-nonce\'], \'custom-dropdown\'))
        return;

    $brands = array_map(\'intval\', $_POST[\'custombrands\']);
    wp_set_object_terms($post_id, $brands, \'brands\');
}

add_action(\'wp_ajax_get_brand_models\', \'get_brand_models\');
function get_brand_models() {
    check_ajax_referer(\'custom-dropdown\', \'dropdown-nonce\');
    if (isset($_POST[\'custombrand\'])) {
        $models = get_terms( \'brands\', \'hide_empty=0&child_of=\'. $_POST[\'custombrand\']);
        echo "<option value=\'0\'>Select one</option>";
        foreach ($models as $model) {
            echo "<option value=\'{$model->term_id}\'>{$model->name}</option>";
        }
    }
    die();
}

结束

相关推荐

是否限制访问wp-admin区域?

我必须检查或比较$pagenow的哪些操作或筛选器?在管理区域,我需要让非管理和编辑器用户只访问:index.php (dashboard) upload.php (attachments management) media.php (attachments management) media-new.php (attachments management) 提前谢谢。