自定义分类大型列表未在选择框中正确加载

时间:2011-07-12 作者:Rajeev

我有与书籍关联的自定义帖子类型“Book”和自定义分类法“Author”。我使用remove\\u meta\\u box来隐藏分类法的默认UI,因为它就像标签一样,您可以添加多个标签并为post选择多个标签。

我想要的是,这本书只有一位作者。所以我使用add\\u meta\\u box为作者创建选择框,管理员可以从列表中选择一位作者。

现在作者列表已超过3000名作者,选择框似乎无法正确加载。。。。。

我不想回到原始的UI类标签。。。。

有什么地方我可以在元框中提供搜索、分页和添加功能(只添加一个分类法)。。。。

这是我正在使用的代码。。

remove_meta_box(\'tagsdiv-book_author\', \'book\', \'normal\');

add_meta_box(\'custom-taxonomy-author-dropdown\',\'Author\',\'author_dropdowns_box\',\'book\',\'side\',\'high\');

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

     echo "Author:";
    echo "<select id=\'bauthor\' name=\'bauthor[]\'>";
    echo "<option value=\'0\'>None</option>";
    foreach ( $terms as $term ) {

            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 />";

}

add_action(\'save_post\',\'save_my_custom_taxonomy\');

1 个回复
SO网友:Bainternet

正如我所评论的,如果下拉列表不存在,这将创建下拉列表,并将其保存在数据库中

    remove_meta_box(\'tagsdiv-book_author\', \'book\', \'normal\');

add_meta_box(\'custom-taxonomy-author-dropdown\',\'Author\',\'author_dropdowns_box\',\'book\',\'side\',\'high\');

function author_dropdowns_box( $post ) {
    wp_nonce_field(\'custom-author-dropdown\', \'author-dropdown-nonce\');

    echo "Author:";
    echo "<select id=\'bauthor\' name=\'bauthor[]\'>";
    echo "<option value=\'0\'>None</option>";
    $options = get_option(\'bauthor_dropdown\');
    if ($option !== false){
        echo $options;
    }else{
        $options = \'\';
        $terms = get_terms( \'book_author\', \'hide_empty=0\');
        $object_terms = wp_get_object_terms( $post->ID, \'book_author\', array(\'fields\'=>\'ids\'));
        foreach ( $terms as $term ) {

                if ( in_array($term->term_id, $object_terms) ) {
                    $parent_id = $term->term_id;
                    $options .= "<option value=\'".$term->term_id."\' selected=\'selected\'>".$term->name."</option>";
                } else {
                    $options .= "<option value=\'".$term->term_id."\'>".$term->name."</option>";
                }

        }
        update_option(\'bauthor_dropdown\',$options);
        echo $options;
    }
    echo "</select><br />";

}

add_action(\'save_post\',\'save_my_custom_taxonomy\');
剩下的就是在添加/编辑/删除该分类法的术语时清除列表:

//clear the list when a new term is created: or edited
add_action(\'edit_book_author\',\'clear_on_add\',10,2);
add_action(\'created_book_author\',\'clear_on_add\',10,2);
function clear_on_add($term_id, $tt_id){
    delete_option(\'bauthor_dropdown\');
}

//clear the list when a term is deleted:
add_action(\'delete_term\', \'clear_on_delete\',10,3);
function clear_on_delete($term, $tt_id, $taxonomy){
    if ($taxonomy == "book_author"){
        delete_option(\'bauthor_dropdown\');
    }
}

结束

相关推荐