簡體   English   中英

將類別選擇器小部件添加到PAGE編輯器中,其中列出了預定義的類別?

[英]Add the categories selector widget to the PAGE editor with predefined categories listed?

以下代碼將類別選擇器小部件添加到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');
}

我想做的是弄清楚如何修改結果類別列表,使其僅包含我定義的硬編碼類別的預定義列表。

由於我是通過自定義主題添加的,因此只有當我的主題在網站上處於活動狀態時,它才會出現在頁面編輯器中。 我有一些特定的“處理程序”類別,我的主題安裝在網站上,以后用於確定布局元素。 我只希望在類別小部件的此特定實例中列出這些特定類別。

使用修改后的版本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');
}

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM