这里有一个功能齐全的代码片段。
我添加了正确的WordPress表格样式这需要添加我的smyles_get_taxonomy_hierarchy
要将分类法的子对象添加到术语对象,如果不存在子对象,则会隐藏子下拉列表,这将根据TERM ID
不是NAME
.. 您应该始终使用术语ID,就好像您决定以后更改措辞/slug一样,您将遇到各种问题
这里的代码语法highlighter似乎无法处理HTML/JS/PHP的混合,所以这里有一个GitHub工作代码要点:
https://gist.github.com/tripflex/527dd82db1d1f3bf82f761486fcc3303细分:
首先需要包含my函数,以生成该术语对象中包含子对象的分类法数组:
if ( ! function_exists( \'smyles_get_taxonomy_hierarchy\' ) ) {
/**
* Recursively get taxonomy and its children
*
* @param string $taxonomy
* @param int $parent Parent term ID (0 for top level)
* @param array $args Array of arguments to pass to get_terms (to override default)
*
* @return array
*/
function smyles_get_taxonomy_hierarchy( $taxonomy, $parent = 0, $args = array( \'hide_empty\' => false ) ) {
$defaults = array(
\'parent\' => $parent,
\'hide_empty\' => false
);
$r = wp_parse_args( $args, $defaults );
// get all direct decendants of the $parent
$terms = get_terms( $taxonomy, $r );
// prepare a new array. these are the children of $parent
// we\'ll ultimately copy all the $terms into this new array, but only after they
// find their own children
$children = array();
// go through all the direct decendants of $parent, and gather their children
foreach ( $terms as $term ) {
// recurse to get the direct decendants of "this" term
$term->children = smyles_get_taxonomy_hierarchy( $taxonomy, $term->term_id );
// add the term to our new array
$children[ $term->term_id ] = $term;
}
// send the results back to the caller
return $children;
}
}
jQuery/JavaScript代码处理:
jQuery( function($){
// slcustom_categories var should be available here
$(\'#parent_category\').change( function(e){
var child_cat_select = $( \'#child_category\' );
var term_id = $(this).val();
console.log( \'Parent Category Changed\', term_id );
// Remove any existing
child_cat_select.find( \'option\' ).remove();
// Loop through children and add to children dropdown
if( slcustom_categories && slcustom_categories[ term_id ] && slcustom_categories[ term_id ][\'children\'] ){
console.log( \'Adding Children\', slcustom_categories[ term_id ][\'children\'] );
$.each( slcustom_categories[term_id][\'children\'], function( i, v ){
console.log( \'Adding Child: \', v );
child_cat_select.append( \'<option value="\' + v[\'term_id\'] + \'">\' + v[ \'name\' ] + \'</option>\');
});
// Show if child cats
$( \'#child_category_row\' ).show();
} else {
// Hide if no child cats
$( \'#child_category_row\' ).hide();
}
});
// Trigger change on initial page load to load child categories
$(\'#parent_category\').change();
});
输出字段的函数:
function slcustom_user_profile_fields( $user ){
$categories = smyles_get_taxonomy_hierarchy( \'project_category\' );
$parent_category = $user->parent_category;
$child_category = $user->child_category;
// $parent_category = 52; // used for testing
// $child_category = 82; // used for testing
$parent_has_children = ! empty( $parent_category ) && $categories[ $parent_category ] && ! empty( $categories[ $parent_category ]->children );
// Creative way to use wp_localize_script which creates a JS variable from array
// You should actually change this to load your JavaScript file and move JS below to that file
wp_register_script( \'slcustom_user_profile_fields\', \'\' );
wp_localize_script( \'slcustom_user_profile_fields\', \'slcustom_categories\', $categories );
wp_enqueue_script( \'slcustom_user_profile_fields\' );
?>
<h1 id="temppp">Select a parent taxonomy</h1>
<table class="form-table">
<tbody>
<tr>
<th>
Parent
</th>
<td>
<select name="parent_category" id="parent_category">
<?php
foreach( (array) $categories as $term_id => $cat ){
?>
<option value="<?php echo esc_attr( $term_id ) ?>"<?php echo selected( $parent_category, $term_id ); ?>><?php echo $cat->name; ?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr id="child_category_row" style="<?php if( ! $parent_has_children ){ echo \'display: none;\'; }?>">
<th>
Child
</th>
<td>
<select name="child_category" id="child_category">
<?php
if( $parent_has_children ){
foreach( (array) $categories[$parent_category]->children as $c_term_id => $child ){
?>
<option value="<?php echo esc_attr( $c_term_id ) ?>"<?php echo selected( $child_category, $c_term_id ); ?>><?php echo $child->name; ?></option>
<?php
}
}
?>
</select>
</td>
</tr>
</tbody>
</table>
<?php
}