你已经走上了正确的道路;只需为$parent_terms
围绕现有循环排列。此外,检查数组中的数字是否为有效的术语ID也是一个好主意,因此最终可能会执行以下操作:
$parent_terms = array( 1, 2, 3, 10 );
$taxonomy = \'products\';
echo \'<ul>\';
foreach ( $parent_terms as $parent_term_id ) {
$parent_term = get_term_by( \'id\', $parent_term_id, $taxonomy );
if ( $parent_term ) {
echo \'<li><a href="\' . get_term_link( $parent_term_id, $taxonomy ) . \'">\' . $parent_term->name . \'</a>\';
$child_terms = get_term_children( $parent_term_id, $taxonomy );
if ( $child_terms ) {
echo \'<ul>\';
foreach ( $child_terms as $child_term_id ) {
$child_term = get_term_by( \'id\', $child_term_id, $taxonomy );
echo \'<li><a href="\' . get_term_link( $child_term_id, $taxonomy ) . \'">\' . $child_term->name . \'</a></li>\';
} // end of child terms loop
echo \'</ul>\';
} // end of $child_terms
echo \'</li>\';
} // end of $parent_term
} // end of parent terms loop
echo \'</ul>\';
Update: 假设您使用的是OptionTree插件,那么您可以填充
$parent_terms
如下数组:
$parent_terms = array(); // define empty array
$term_options = ot_get_option(\'option-name\');
foreach ( $term_options as $term_option_id ) {
// convert strings to numbers and save them in $parent_terms
array_push( $parent_terms, intval($term_option_id) );
}