![](/img/trans.png)
[英]PHP - Wordpress Plugin to add predefined categories to custom taxonomy
[英]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.