多级别类别下拉列表

时间:2011-02-27 作者:Zach Shallbetter

我使用WordPress作为CMS,希望有多个选项中的类别可供选择,即<option>.

enter image description hereenter image description here

作为使用示例,第一个下拉列表将包含父类别。然后,在选择父类别后,将显示第二个选择下拉列表,其中包含子类别。Wordpress上有几个可用的插件。但它们似乎都已损坏或过时。任何帮助都将不胜感激。

断开的插件http://wordpress.org/extend/plugins/ajax-category-dropdown/

我使用的代码来自wordpress。组织

<form action="<?php bloginfo(\'url\'); ?>/" method="get">
    <?php
    $select = wp_dropdown_categories(\'orderby=name&echo=0&depth=0&hierarchical=1&exclude=5,4\');
    $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange=\'return this.form.submit()\'>", $select);
    echo $select;
    ?>
    <noscript><input type="submit" value="View" /></noscript>
</form>

2 个回复
SO网友:Ján Bočínec

function parent_child_cat_select() { ?>
        <script type="text/javascript">
        /* <![CDATA[ */
            jQuery(document).ready(function() {     
                jQuery(\'#parent_cat\').change(function(){
                    var parentCat=jQuery(\'#parent_cat\').val();
                    // call ajax
                    jQuery.ajax({
                        url:"/wp-admin/admin-ajax.php",
                        type:\'POST\',
                        data:\'action=category_select_action&parent_cat_ID=\' + parentCat,
                        success:function(results)
                        {
                        jQuery("#sub_cat_div").html(results);
                        }
                     });
                });         
            });     
        /* ]]> */
        </script>

        <form action="<?php bloginfo(\'url\'); ?>/" method="get">

        <div id="parent_cat_div"><?php wp_dropdown_categories("show_option_none=Select parent category&orderby=name&depth=1&hierarchical=1&id=parent_cat"); ?></div>

        <div id="sub_cat_div"><select name="sub_cat_disabled" id="sub_cat_disabled" disabled="disabled"><option>Select parent category first!</option></select></div>

        <div id="submit_div"><input type="submit" value="View" /></div>

        </form>
<?php }

function implement_ajax() {
    $parent_cat_ID = $_POST[\'parent_cat_ID\'];
    if ( isset($parent_cat_ID) )
    {
        $has_children = get_categories("parent=$parent_cat_ID");
        if ( $has_children ) {
            wp_dropdown_categories("orderby=name&parent=$parent_cat_ID");
        } else {
            ?><select name="sub_cat_disabled" id="sub_cat_disabled" disabled="disabled"><option>No child categories!</option></select><?php
        }
        die();
    } // end if
}
add_action(\'wp_ajax_category_select_action\', \'implement_ajax\');
add_action(\'wp_ajax_nopriv_category_select_action\', \'implement_ajax\');//for users that are not logged in.

//this is optional, only if you are not already using jQuery  
function load_jquery() {
    wp_enqueue_script(\'jquery\');            
}     
add_action(\'init\', \'load_jquery\');
要显示下拉菜单,请使用parent_child_cat_select() 作用

<?php parent_child_cat_select(); ?>

SO网友:wyrfel

<form action="<?php echo remove_query_arg(array(\'mycat_go\', \'cat\')); ?>" method="get">
    <?php
    // get selected category and do some initializations
    if (isset($_REQUEST[\'cat\'])) $_REQUEST[\'mycat_go\'] = $_REQUEST[\'cat\'];
    $selected = $_REQUEST[\'mycat_go\'];
    $selects = array();
    $last = 0;
    $top = false;
    $i = 0;
    // basically, we are looping from the selected up through the parents
    // till we have no parent anymore.
    while (!$top) {
        // prep query to generate field containing all child categories
        // of the selected one
        $args = array(
            \'name\' => \'mycat_\'.$i,
            \'orderby\' => \'name\',
            \'echo\' => 0,
            \'hierarchical\' => 1,
            \'exclude\' => \'4,5\',
            \'child_of\' => $selected,
            \'depth\' => 1,
            \'show_option_none\' => \'--select--\',
            \'hide_if_empty\' => true,
        );
        if(!empty($last)) $args[\'selected\'] = $last;
        // prepare next loop iteration or stop if we are displaying children of 0
        if (!empty($selected)) {
            $last = $selected;
            $category = get_category($selected);
            $selected = $category->parent;
        } else {
            $top = true;
        }
        // generate output and store in reversed order as we are going bottom up
        $select = wp_dropdown_categories($args);
        $select = preg_replace("#<option([^>]*)>#", "<option$1 onclick=\\"this.parentNode.name = \'mycat_go\';return this.form.submit()\\">", $select);
        array_unshift($selects, $select);
        $i++;
    }
    // print to screen
    foreach ($selects as $select) {
        echo $select;
    }
    ?>
</form>
<form action="<?php remove_query_arg(array(\'mycat_go\', \'cat\')); ?>" method="get">
    <input type="hidden" name="cat" value="<?php echo $_REQUEST[\'mycat_go\']; ?>" />
    <input type="submit" value="Go there" />
</form>
可能需要一些抛光,但应该可以。

结束

相关推荐

在WordPress中筛选分类术语的Get_Categories()

我只想在(自定义)帖子属于某个类别且region=该帖子的$name时显示该类别。因此,例如,我有一个自定义帖子(类型“business”),在子类别“pizzerias”(父类别“food”)和区域“Rotterdam”(自定义分类:“region”,自定义分类术语:“Rotterdam”)中分别命名为“Mamma Mia”。=>显示类别“pizzerias”(和父类别“food”)只是,我不知道如何做到这一点。如果你能帮我解决这个难题,我将不胜感激。<?php // $