在编辑帖子页面上以下拉列表的形式显示自定义分类

时间:2012-04-24 作者:David Gard

创建自定义分类法时,似乎唯一的选项是将其显示为标记(非层次)或类别(层次)。有没有办法将它们显示为下拉菜单?

我意识到这可以通过add_meta_box() 实际上手动添加它,但我正在寻找一种内置方式(如果存在的话),以保存大量代码!谢谢

5 个回复
最合适的回答,由SO网友:Stephen Harris 整理而成

不幸的是,没有办法做到这一点wp_register_taxonomy(). 注册分类法时,默认元框为复选框(层次)或云(非层次)。

解决此问题的唯一方法是取消注册现有metabox,并在其位置添加一个新的metabox。通过仔细模仿“默认”元数据库,您不必自己处理数据,但可以让WordPress自动处理添加/删除术语。

有关如何执行此操作的详细信息,请参见this answer. 你也可以找到这个GitHub repository 有帮助(现在包括在新的metabox中动态创建术语的功能)。链接的资源指的是创建带有单选框的元盒,但它可以很容易地适应下拉菜单。

SO网友:Alexufo

自wp 3.8以来,您可以通过meta_box_cb

“drop\\u cat()”-由重建原始核心类别metaboxwp admin/includes/meta框组成。php

function realty_type() {
$args = array(
    \'show_ui\'                    => true,
    \'meta_box_cb\'                => \'drop_cat\',
);
register_taxonomy( \'realty_type\', array( \'YOUR_POST_TYPE\' ), $args );

}

// Hook into the \'init\' action
add_action( \'init\', \'realty_type\', 0 );


function drop_cat( $post, $box ) {
$defaults = array(\'taxonomy\' => \'category\');
if ( !isset($box[\'args\']) || !is_array($box[\'args\']) )
    $args = array();
else
    $args = $box[\'args\'];
extract( wp_parse_args($args, $defaults), EXTR_SKIP );
$tax = get_taxonomy($taxonomy);
?>
<div id="taxonomy-<?php echo $taxonomy; ?>" class="acf-taxonomy-field categorydiv">

        <?php 
        $name = ( $taxonomy == \'category\' ) ? \'post_category\' : \'tax_input[\' . $taxonomy . \']\';
        echo "<input type=\'hidden\' name=\'{$name}[]\' value=\'0\' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
        ?>
        <? $term_obj = wp_get_object_terms($post->ID, $taxonomy ); //_log($term_obj[0]->term_id)?>
        <ul id="<?php echo $taxonomy; ?>checklist" data-wp-lists="list:<?php echo $taxonomy?>" class="categorychecklist form-no-clear">
            <?php //wp_terms_checklist($post->ID, array( \'taxonomy\' => $taxonomy) ) ?>
        </ul>

        <?php wp_dropdown_categories( array( \'taxonomy\' => $taxonomy, \'hide_empty\' => 0, \'name\' => "{$name}[]", \'selected\' => $term_obj[0]->term_id, \'orderby\' => \'name\', \'hierarchical\' => 0, \'show_option_none\' => \'&mdash;\' ) ); ?>

</div>
<?php
}

SO网友:Wordpressor

给你:

<select>
    <?php
       $tax_terms = get_terms(\'your_taxonomy_name\', array(\'hide_empty\' => \'0\'));      
       foreach ( $tax_terms as $tax_term ):  
          echo \'<option value="\'.$tax_term->name.\'">\'.$tax_term->name.\'</option>\';   
       endforeach;
    ?>
</select> 
如果不想显示空条款,请删除$tax\\u terms第二个参数。

同时使用selected 如果要保存拾取的选项:)

SO网友:terry e

我对Alexufo提供的代码有困难,所以我根据WP 4.1.2中更新的函数重写了它,下面是我更新的drop\\u cat函数:

//function below re-purposed from wp-admin/includes/meta-boxes.php - post_categories_meta_box()
function drop_cat( $post, $box ) {
    $defaults = array( \'taxonomy\' => \'category\' );
    if ( ! isset( $box[\'args\'] ) || ! is_array( $box[\'args\'] ) ) {
        $args = array();
    } else {
        $args = $box[\'args\'];
    }
    $r = wp_parse_args( $args, $defaults );
    $tax_name = esc_attr( $r[\'taxonomy\'] );
    $taxonomy = get_taxonomy( $r[\'taxonomy\'] );
    ?>
    <div id="taxonomy-<?php echo $tax_name; ?>" class="categorydiv">

    <?php //took out tabs for most recent here ?>

        <div id="<?php echo $tax_name; ?>-all">
            <?php
            $name = ( $tax_name == \'category\' ) ? \'post_category\' : \'tax_input[\' . $tax_name . \']\';
            echo "<input type=\'hidden\' name=\'{$name}[]\' value=\'0\' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
            ?>
            <ul id="<?php echo $tax_name; ?>checklist" data-wp-lists="list:<?php echo $tax_name; ?>" class="categorychecklist form-no-clear">
                <?php //wp_terms_checklist( $post->ID, array( \'taxonomy\' => $tax_name, \'popular_cats\' => $popular_ids ) ); ?>
            </ul>

            <?php $term_obj = wp_get_object_terms($post->ID, $tax_name ); //_log($term_obj[0]->term_id) ?>
            <?php wp_dropdown_categories( array( \'taxonomy\' => $tax_name, \'hide_empty\' => 0, \'name\' => "{$name}[]", \'selected\' => $term_obj[0]->term_id, \'orderby\' => \'name\', \'hierarchical\' => 0, \'show_option_none\' => "Select $tax_name" ) ); ?>

        </div>
    <?php if ( current_user_can( $taxonomy->cap->edit_terms ) ) : 
            // removed code to add terms here dynamically, because doing so added a checkbox above the newly added drop menu, the drop menu would need to be re-rendered dynamically to display the newly added term ?>
        <?php endif; ?>

        <p><a href="<?php echo site_url(); ?>/wp-admin/edit-tags.php?taxonomy=<?php echo $tax_name ?>&post_type=YOUR_POST_TYPE">Add New</a></p>
    </div>
    <?php
}
我遇到的一个问题(正如我在上述函数中的评论所指出的那样)是,我无法在元框中使用WP core添加术语/类别功能,因为它默认为在我的选择菜单上方的复选框中动态添加新术语。为了使用选择菜单,添加新术语后需要重新渲染菜单。我肯定我能弄明白,但我会再等一天。

SO网友:henok tsegaye

上面的代码对我来说有一些问题,让我把适合我的代码

 \'meta_box_cb\' => \'drop_cat\',
您的类别定义,然后将此代码放在此处

记住把

function drop_cat( $post, $box ) {
    if ( ! isset( $box[\'args\'] ) || ! is_array( $box[\'args\'] ) ) {
        $args = array();
    } else {
        $args = $box[\'args\'];
    }
    $r = wp_parse_args( $args, $defaults );
    $tax_name = esc_attr( $r[\'taxonomy\'] );
    $taxonomy = get_taxonomy( $r[\'taxonomy\'] );
    ?>
    <div id="taxonomy-<?php echo $tax_name; ?>" class="categorydiv">

    <?php //took out tabs for most recent here ?>

        <div id="<?php echo $tax_name; ?>-all">
            <?php
            $name = ( $tax_name == \'category\' ) ? \'post_category\' : \'tax_input[\' . $tax_name . \']\';
            echo "<input type=\'hidden\' name=\'{$name}[]\' value=\'0\' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
            ?>
            <ul id="<?php echo $tax_name; ?>checklist" data-wp-lists="list:<?php echo $tax_name; ?>" class="categorychecklist form-no-clear">
                <?php //wp_terms_checklist( $post->ID, array( \'taxonomy\' => $tax_name, \'popular_cats\' => $popular_ids ) ); ?>
            </ul>

            <?php $term_obj = wp_get_object_terms($post->ID, $tax_name ); //_log($term_obj[0]->term_id) 
          
            ?>
            <?php wp_dropdown_categories( array( \'taxonomy\' => $tax_name, \'option_none_value\' => 0 ,\'id\'=>$term_obj[0]->term_id, \'value_field\'=> \'slug\' , \'hide_empty\' => 0, \'name\' => "{$name}[]", \'selected\' => $term_obj[0]->slug, \'orderby\' => \'name\', \'hierarchical\' => 0, \'show_option_none\' => "Select $tax_name" ) ); ?>

        </div>
    <?php if ( current_user_can( $taxonomy->cap->edit_terms ) ) : 
            // removed code to add terms here dynamically, because doing so added a checkbox above the newly added drop menu, the drop menu would need to be re-rendered dynamically to display the newly added term ?>
        <?php endif; ?>

        <p><a href="<?php echo site_url(); ?>/wp-admin/edit-tags.php?taxonomy=<?php echo $tax_name ?>&post_type=YOUR_POST_TYPE">Add New</a></p>
    </div>
    <?php
    }

结束