Wordpress - 帖子编辑页面中的类别列表顺序 [英] Wordpress - Category list order in post edit page

查看:21
本文介绍了Wordpress - 帖子编辑页面中的类别列表顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想停止 WordPress 在管理 > 帖子编辑页面中重新排序类别列表.默认行为是将分配给帖子的类别从其自然的父/子流中取出,并将它们放在列表的顶部.我想阻止这种情况发生,因为当类别结构很大时它会令人困惑.

有什么想法吗?

谢谢.

解决方案

您使用的是什么版本的 Wordpress?Wordpress 3.04 在帖子编辑页面上提供了父/子树.您确定您也没有查看最常用"标签吗?

没关系,我看到的正是你所说的问题,在帖子保存后出现:

好的,尝试将其粘贴到主题中的 functions.php 中:

//移除旧框函数 remove_default_categories_box() {remove_meta_box('categorydiv', 'post', 'side');}add_action('admin_head', 'remove_default_categories_box');//添加新框函数 add_custom_categories_box() {add_meta_box('customcategorydiv', 'Categories', 'custom_post_categories_meta_box', 'post', 'side', 'low', array('taxonomy' => 'category'));}add_action('admin_menu', 'add_custom_categories_box');/*** 显示自定义帖子类别表单字段.** @自 2.6.0 起** @param 对象 $post*/函数 custom_post_categories_meta_box( $post, $box ) {$defaults = array('taxonomy' => 'category');if ( !isset($box['args']) || !is_array($box['args']) )$args = array();别的$args = $box['args'];提取(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('最常被使用' );?></a></li><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);?>

<div id="<?php echo $taxonomy; ?>-all" class="tabs-panel"><?php$name = ( $taxonomy == 'category' ) ?'post_category' : 'tax_input[' .$分类法.']';echo "<input type='hidden' name='{$name}[]' value='0'/>";//允许发送空术语集.0 是无效的术语 ID,将被 empty() 检查忽略.?><ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy?> categorychecklist form-no-clear"><?php/*** 这是我们必须在原始函数中更改的一行* 请注意,checked_ontop"现在设置为 FALSE*/wp_terms_checklist($post->ID, array('taxonomy' => $taxonomy, 'popular_cats' => $popular_ids, 'checked_ontop' => FALSE)) ?>

<?php if ( !current_user_can($tax->cap->assign_terms) ) : ?><p><em><?php _e('你不能修改这个分类法.');?></em></p><?php endif;?><?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/* 翻译器:%s:添加新的分类标签 */printf( __( '+ %s' ), $tax->labels->add_new_item );?></a><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"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;?><?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 $分类法?>-添加按钮类别-add-sumbit" value="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>

<?php endif;?>

<?php}

唯一真正的变化是添加了 'checked_ontop' =>FALSEwp_terms_checklist() 函数中的参数在混乱中.其他一切都是原始的 post_categories_meta_box() 函数.

(你可以修改/wp-admin/includes/meta-boxes.php中原来的post_categories_meta_box(),但不建议乱用core并且添加/删除操作是正确的方法.

I want to stop WordPress re-ordering the category list in the admin > post edit page. The default behaviour is to take the categories assigned to the post out of their natural parent/child flow and put them at the top of the list. I want to stop this happening as it is confusing when the category structure is big.

Any thoughts?

Thanks.

解决方案

What version of Wordpress are you using? Wordpress 3.04 provides the Parent/Child tree on the Post Edit page. Are you sure you also aren't viewing the "Most Used" tab?

Nevermind, I see exactly the problem you are talking about, which show up after the post is saved:

Alright, try pasting this into the functions.php in your theme:

// remove the old box
function remove_default_categories_box() {
    remove_meta_box('categorydiv', 'post', 'side');
}
add_action( 'admin_head', 'remove_default_categories_box' );

// add the new box
function add_custom_categories_box() {
    add_meta_box('customcategorydiv', 'Categories', 'custom_post_categories_meta_box', 'post', 'side', 'low', array( 'taxonomy' => 'category' ));
}
add_action('admin_menu', 'add_custom_categories_box');

/**
 * Display CUSTOM post categories form fields.
 *
 * @since 2.6.0
 *
 * @param object $post
 */
function custom_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 
                /**
                 * This is the one line we had to change in the original function
                 * Notice that "checked_ontop" is now set to FALSE
                 */
                wp_terms_checklist($post->ID, array( 'taxonomy' => $taxonomy, 'popular_cats' => $popular_ids, 'checked_ontop' => FALSE ) ) ?>
            </ul>
        </div>
    <?php if ( !current_user_can($tax->cap->assign_terms) ) : ?>
    <p><em><?php _e('You cannot modify this taxonomy.'); ?></em></p>
    <?php endif; ?>
    <?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
}

The only real change was adding 'checked_ontop' => FALSE to the args in wp_terms_checklist() function in the middle of that mess. Everything else is the original post_categories_meta_box() function.

(You could just modify the original post_categories_meta_box() in /wp-admin/includes/meta-boxes.php, but it is not recommended to mess with core and adding/removing actions as above is the proper way to do it.

这篇关于Wordpress - 帖子编辑页面中的类别列表顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆