添加帖子屏幕保留类别结构

时间:2012-08-15 作者:Kyle Williams

因此,在wordpress中的添加帖子屏幕上有类别复选框div。在为帖子选择类别之前,它会显示类别管理屏幕中定义的正确层次结构。选择类别并保存帖子后,它会将您选择的类别放在列表的顶部,而不是保留结构化布局。有没有办法阻止它这样做?

2 个回复
最合适的回答,由SO网友:Tom Auger 整理而成

是的。

使用add_meta_boxes_post 要添加保留结构的新类别元盒,请删除默认元盒,如下所示:

add_action( \'add_meta_boxes_post\', \'fix_hierarchical_post_categories_metaboxes\', 10, 1 );
下面是回调:

/*
 * This include provides an alternative post category metabox that sets the checked_ontop argument of wp_terms_checklist to false.
 * This prevents the re-ordering of selected category items to the top of the list, instead preserving their hierarchical
 * order.
 */

function fix_hierarchical_post_categories_metaboxes( $post ){
    global $wp_meta_boxes;
    foreach ( $wp_meta_boxes as &$post_metaboxes ){
        foreach ( $post_metaboxes as &$position_metaboxes ){
            foreach ( $position_metaboxes as &$priority_metaboxes ){
                foreach ( $priority_metaboxes as &$metabox ){
                    if ( $metabox[\'callback\'] == \'post_categories_meta_box\' ){
                        $metabox[\'callback\'] = \'hierarchical_post_categories_meta_box\';
                    }
                }
            }
        }
    }
}

function hierarchical_post_categories_meta_box( $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="categorydiv">
        <ul id="<?php echo $taxonomy; ?>-tabs" class="category-tabs">
            <li class="tabs"><a href="#<?php echo $taxonomy; ?>-all" tabindex="3"><?php echo $tax->labels->all_items; ?></a></li>
            <li class="hide-if-no-js"><a href="#<?php echo $taxonomy; ?>-pop" tabindex="3"><?php _e( \'Most Used\' ); ?></a></li>
        </ul>

        <div id="<?php echo $taxonomy; ?>-pop" class="tabs-panel" style="display: none;">
            <ul id="<?php echo $taxonomy; ?>checklist-pop" class="categorychecklist form-no-clear" >
                <?php $popular_ids = wp_popular_terms_checklist($taxonomy); ?>
            </ul>
        </div>

        <div id="<?php echo $taxonomy; ?>-all" class="tabs-panel">
            <?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.
            ?>
            <ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy?> categorychecklist form-no-clear">
                <?php wp_terms_checklist($post->ID, array( \'taxonomy\' => $taxonomy, \'popular_cats\' => $popular_ids, \'checked_ontop\' => false ) ) ?>
            </ul>
        </div>
    <?php if ( current_user_can($tax->cap->edit_terms) ) : ?>
            <div id="<?php echo $taxonomy; ?>-adder" class="wp-hidden-children">
                <h4>
                    <a id="<?php echo $taxonomy; ?>-add-toggle" href="#<?php echo $taxonomy; ?>-add" class="hide-if-no-js" tabindex="3">
                        <?php
                            /* translators: %s: add new taxonomy label */
                            printf( __( \'+ %s\' ), $tax->labels->add_new_item );
                        ?>
                    </a>
                </h4>
                <p id="<?php echo $taxonomy; ?>-add" class="category-add wp-hidden-child">
                    <label class="screen-reader-text" for="new<?php echo $taxonomy; ?>"><?php echo $tax->labels->add_new_item; ?></label>
                    <input type="text" name="new<?php echo $taxonomy; ?>" id="new<?php echo $taxonomy; ?>" class="form-required form-input-tip" value="<?php echo esc_attr( $tax->labels->new_item_name ); ?>" tabindex="3" aria-required="true"/>
                    <label class="screen-reader-text" for="new<?php echo $taxonomy; ?>_parent">
                        <?php echo $tax->labels->parent_item_colon; ?>
                    </label>
                    <?php wp_dropdown_categories( array( \'taxonomy\' => $taxonomy, \'hide_empty\' => 0, \'name\' => \'new\'.$taxonomy.\'_parent\', \'orderby\' => \'name\', \'hierarchical\' => 1, \'show_option_none\' => \'&mdash; \' . $tax->labels->parent_item . \' &mdash;\', \'tab_index\' => 3 ) ); ?>
                    <input type="button" id="<?php echo $taxonomy; ?>-add-submit" class="add:<?php echo $taxonomy ?>checklist:<?php echo $taxonomy ?>-add button category-add-sumbit" value="<?php echo esc_attr( $tax->labels->add_new_item ); ?>" tabindex="3" />
                    <?php wp_nonce_field( \'add-\'.$taxonomy, \'_ajax_nonce-add-\'.$taxonomy, false ); ?>
                    <span id="<?php echo $taxonomy; ?>-ajax-response"></span>
                </p>
            </div>
        <?php endif; ?>
    </div>
    <?php
}
第一个函数负责在默认情况下交换metabox回调post_categories_meta_box 为我们的新产品。

第二个功能基本上是复制和粘贴原件post_categories_meta_box 回调,我修改了将所选类别置于顶部的部分。

似乎有很多工作要做,嗯?

SO网友:Rachel Carden

“wp\\u terms\\u checklist\\u args”过滤器可能早在2012年8月就不存在了,但现在有了一种更简单的方法来确保选中的术语保持不变。

此筛选器在wp\\u terms\\u checklist()函数中调用,您可以筛选用于创建清单的参数:

add_filter( \'wp_terms_checklist_args\', \'my_website_wp_terms_checklist_args\', 1, 2 );
function my_website_wp_terms_checklist_args( $args, $post_id ) {

   $args[ \'checked_ontop\' ] = false;

   return $args;

}

结束

相关推荐

Get_Categories仅获取主语言的类别

我试图在“content composer”中检索基于活动语言的项目类别。但我只从主要语言中获取类别,而没有从其他语言中获取类别。我的代码是:类别全部 $categories = get_categories(\'taxonomy=jw_portfolio_categories\'); if(!empty($categories)){ foreach($categories