【问题标题】:Add the categories selector widget to the PAGE editor with predefined categories listed?将类别选择器小部件添加到 PAGE 编辑器并列出预定义类别?
【发布时间】:2010-03-27 12:59:48
【问题描述】:

以下代码会将类别选择器小部件添加到 WordPress 页面编辑器界面...

add_action('admin_menu', 'my_post_categories_meta_box');
function my_post_categories_meta_box() {
    add_meta_box('categorydiv', __('Categories'), 'post_categories_meta_box', 'page', 'side', 'core');
}

我想做的是弄清楚如何修改生成的类别列表,使其仅包含我定义的硬编码类别的预定义列表。

由于我是通过我的自定义主题添加的,因此只有当我的主题在网站上处于活动状态时,它才会出现在页面编辑器中。我有一些特定的“处理程序”类别,我的主题安装到站点中,然后用于确定布局元素。我只希望这些特定类别在类别小部件的这个特定实例中列出。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    使用post_categories_meta_box 的修改版本,其中将对wp_category_checklist 的调用更改为修改版本wp_category_checklist_modified

    post_categories_meta_box_modified

    function post_categories_meta_box_modified($post) {
    ?>
    <ul id="category-tabs">
        <li class="ui-tabs-selected"><a href="#categories-all" tabindex="3"><?php _e( 'All Categories' ); ?></a></li>
        <li class="hide-if-no-js"><a href="#categories-pop" tabindex="3"><?php _e( 'Most Used' ); ?></a></li>
    </ul>
    
    <div id="categories-pop" class="ui-tabs-panel" style="display: none;">
        <ul id="categorychecklist-pop" class="categorychecklist form-no-clear" >
            <?php $popular_ids = wp_popular_terms_checklist('category'); ?>
        </ul>
    </div>
    
    <div id="categories-all" class="ui-tabs-panel">
        <ul id="categorychecklist" class="list:category categorychecklist form-no-clear">
            <?php wp_category_checklist($post->ID, false, array($cat1_id, $cat2_id.... ,$catn_id), $popular_ids) ?>
        </ul>
    </div>
    
    <?php if ( current_user_can('manage_categories') ) : ?>
    <div id="category-adder" class="wp-hidden-children">
        <h4><a id="category-add-toggle" href="#category-add" class="hide-if-no-js" tabindex="3"><?php _e( '+ Add New Category' ); ?></a></h4>
        <p id="category-add" class="wp-hidden-child">
            <label class="hidden" for="newcat"><?php _e( 'Add New Category' ); ?></label><input type="text" name="newcat" id="newcat" class="form-required form-input-tip" value="<?php _e( 'New category name' ); ?>" tabindex="3" aria-required="true"/>
            <label class="hidden" for="newcat_parent"><?php _e('Parent category'); ?>:</label><?php wp_dropdown_categories( array( 'hide_empty' => 0, 'name' => 'newcat_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => __('Parent category'), 'tab_index' => 3 ) ); ?>
            <input type="button" id="category-add-sumbit" class="add:categorychecklist:category-add button" value="<?php _e( 'Add' ); ?>" tabindex="3" />
            <?php wp_nonce_field( 'add-category', '_ajax_nonce', false ); ?>
            <span id="category-ajax-response"></span>
        </p>
    </div>
    <?php
    endif;
    
    }
    

    我只是改变了原来函数的行

    <?php wp_category_checklist($post->ID, false, false, $popular_ids) ?>
    

    <?php wp_category_checklist_modified($post->ID, false, false, $popular_ids) ?>
    

    wp_category_checklist_modified:

    function wp_category_checklist_modified( $post_id = 0, $descendants_and_self = 0, $selected_cats = false, $popular_cats = false, $include_cats = array() ) {
        $walker = new Walker_Category_Checklist;
        $descendants_and_self = (int) $descendants_and_self;
        $cat_ids_list = implode(',', $include_cats);
    
        $args = array();
    
        if ( is_array( $selected_cats ) )
            $args['selected_cats'] = $selected_cats;
        elseif ( $post_id )
            $args['selected_cats'] = wp_get_post_categories($post_id);
        else
            $args['selected_cats'] = array();
    
        if ( is_array( $popular_cats ) )
            $args['popular_cats'] = $popular_cats;
        else
            $args['popular_cats'] = get_terms( 'category', array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
    
        if ( $descendants_and_self ) {
            $categories = get_categories( "child_of=$descendants_and_self&hierarchical=0&hide_empty=0&include=$cat_ids_list" );
            $self = get_category( $descendants_and_self );
            array_unshift( $categories, $self );
        } else {
            $categories = get_categories('get=all&include='. $cat_ids_list);
        }
    
        // Post process $categories rather than adding an exclude to the get_terms() query to keep the query the same across all posts (for any query cache)
        $checked_categories = array();
        for ( $i = 0; isset($categories[$i]); $i++ ) {
            if ( in_array($categories[$i]->term_id, $args['selected_cats']) ) {
                $checked_categories[] = $categories[$i];
                unset($categories[$i]);
            }
        }
    
        // Put checked cats on top
        echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));
        // Then the rest of them
        echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));
    }
    

    这里我为wp_category_checklist_modified$include_cats 添加了一个额外的参数,您可以在其中指定类别ID,然后我在对get_categories 的两次调用中使用此列表作为include 参数传递.

    这些函数没有记录(据我所知),所以我不得不查看源代码。

    那么你就简单地使用

    function my_post_categories_meta_box() {
       add_meta_box('categorydiv', __('Categories'), 'post_categories_meta_box_modified', 'page', 'side', 'core');
    }
    

    希望这会有所帮助。

    【讨论】:

    • 听起来不错,尼科洛。现在就去试试吧:)
    • Nicolo,我只能获得完整的类别列表。不知道我做错了什么。我只想要该列表中的两个类别。这是我的函数调用... ID, false, array($nofollow, $noindex), $popular_ids) ?>
    • @nicolo - 有没有一种方法可以通过 slug 而不是 ID 来指定类别。我对 Cat_id 的问题是这是一个自定义主题,我不知道从安装到安装的 cat_id。我确实知道类别 slug 将始终是“noindex”和“nofollow”
    • 最后一个问题,可以使用get_term_by函数。这应该有效: term_id; ?>。对于前两个,我发现答案是错误的,数组参数只指定了选定的猫……我会更正答案,对不起
    • Nicolo:非常感谢您在这方面的帮助。我确信它会比这更简单:) 我原来的函数是 3 行代码,哈哈。现在要简单地将列表减少到我指定的类别,我必须添加 82 行代码???我不是忘恩负义,我只是说......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多