您好,我有一个问题可能比WordPress更像PHP问题,但在WordPress上下文中可能会很有趣,以防有人想向WP对象添加属性。
我创建了一个自定义分类法,并使用一个选项为每个术语添加顺序。
为了获得主题中的顺序,我使用了这两个函数:第一个函数检索术语,然后对它们进行排序:
// SORT BY ORDER
function cmp( $a, $b ) {
if( $a->order == $b->order ){ return 0 ; }
return ($a->order < $b->order ) ? -1 : 1;
}
// FUNCTION TO INJECT ORDER ITEM IN TERM OBJECT
function inject_order( $term_objects ) {
if ( $term_objects ) {
foreach ( $term_objects as $term_object ) {
$term_id = $term_object->term_id;
$term_order = get_option( "taxonomy_term_$term_id" )[\'tax_order_nb\'];
$term_object->order = $term_order;
array_replace( $term_objects, $term_object );
}
return $term_objects;
} else {
return false;
}
}
// FUNCTION TO GET CHILDREN TERMS AND ORDER THEM
function get_children_and_order_terms( $term_id = \'0\' ) {
$term_children = get_terms( \'category_effectif\', array( \'parent\' => $term_id, \'hide_empty\' => 0 ) );
if ( $term_children ) {
$term_children = inject_order( $term_children );
usort ( $term_children, \'cmp\' );
return $term_children;
}
}
它工作得很好,但我有一个PHP\\u警告
array_replace(): Argument #2 is not an array
我想纠正这个错误,让它变得更好。