terminy – zamówienie niestandardowe get_terms
![terminy – zamówienie niestandardowe get_terms](https://oen.pl/wp-content/uploads/2023/09/apple-touch-icon@2.png)
Można to łatwo zrobić za pomocą własnej funkcji niestandardowej. To, co chcesz tutaj zrobić, to pobrać swój obiekt get_terms()
który będzie przechowywać twoje obiekty terminowe, sprawdź, czy twój unikalny klucz jest ustawiony jako parametr, a następnie zgodnie z tym usuń ten klucz/parę i dodaj go z tyłu
Teraz umieśćmy to w kodzie: (Skomentowałem kod, aby ułatwić jego przestrzeganie)
To jest wersja przed PHP 5.5, ale nadal wymagana jest co najmniej PHP 5.4
function get_term_to_end( $taxonomy = '', $args = [] )
{
$terms = get_terms( $taxonomy, $args );
/*
* Return $terms if our new argument term_to_end is not set or empty
*/
if ( !isset( $args['term_to_end'] ) || empty( $args['term_to_end'] ) )
return $terms;
/*
* If term_to_end is set and has a value, continue to process
* Return $terms if a wp error is returned or if $terms is empty
*/
if ( is_wp_error( $terms ) || empty( $terms ) )
return $terms;
/*
* We have came this far, now we can finish this off
*/
foreach ( $terms as $term ) {
/*
* Check the term ids against our new argument term_to_end
*/
if ( $term->term_id == $args['term_to_end'] ) {
$end[] = $term;
} else {
$terms_array[] = $term;
}
}
/*
* Merge the two arrays together, adding the term_to_end right at the back
*/
$terms = array_merge( $terms_array, $end );
/*
* For uniformaty, type cast the resultant array to an object
*/
return (object) $terms;
}
Jeśli używasz PHP 5.5 +, możesz użyć array_search
I array_column
i pomiń foreach
pętla wewnątrz funkcji
function get_term_to_end( $taxonomy = '', $args = [] )
{
$terms = get_terms( $taxonomy, $args );
/*
* Return $terms if our new argument term_to_end is not set or empty
*/
if ( !isset( $args['term_to_end'] ) || empty( $args['term_to_end'] ) )
return $terms;
/*
* If term_to_end is set and has a value, continue to process
* Return $terms if a wp error is returned or if $terms is empty
*/
if ( is_wp_error( $terms ) || empty( $terms ) )
return $terms;
/*
* We have came this far, now we can finish this off
*
* We need to convert the multidimensional objects to a multidimensional array. Lets used
* json_encode and json_decode. Now we can use
* array_search to and array_column to determine the position of the term_to_end
*/
$terms_array = json_decode( json_encode( $terms ), true );
$end_term_position = array_search( $args['term_to_end'], array_column( $terms_array, 'term_id'));
/*
* First check if $end_term_position in not false (array_search returns false on failure),
* if false, return $terms
*/
if ( !$end_term_position )
return $terms;
/*
* Get the key value pair for term_to_end, unset it from $terms and reset term_to_end pair at the back
*/
$end_term_pair[] = $terms[$end_term_position];
unset( $terms[$end_term_position] );
$new_terms_array = array_merge( (array) $terms, $end_term_pair );
/*
* We are done!! Now we can just return $new_terms_array as an object
*/
return (object) $new_terms_array;
}
Przykładowy przypadek użycia:
Przenieś termin o identyfikatorze 15 należący do taksonomii mytax
na tył tablicy terminów. Zachowaj domyślną nazwę według nazwy
$terms = get_term_to_end( 'mytax', ['term_to_end' => 15] );
Lub
$args = [
'term_to_end' => 15
];
$terms = get_term_to_end( 'mytax, $args );
KILKA UWAG DO POWYŻSZEGO
-
Powyższy kod jest nieprzetestowany i może zawierać błędy. Napisałem to na tablecie.
-
W starszych wersjach (przed PHP 5.4) użyj pierwszego bloku kodu i po prostu zmień nową składnię tablicy (
[]
) do starej składni (array()
) -
Nowy parametr,
term_to_end
używa tego terminu, aby przejść do identyfikatora terminu końcowego. Możesz to zmodyfikować, aby działało z terminem nazwa lub termin ślimak, w zależności od potrzeb. Można to również zmodyfikować, aby pracować ze wszystkimi tymi wartościami -
Nowa funkcja używa dokładnie tych samych parametrów co
get_terms()
. Jedynym dodatkowym dodatkiem jestterm_to_end
parametr -
Jak
get_terms
nowa funkcja zwraca aWP_Error
obiekt, jeśli taksonomia nie istnieje, a także zwraca tablicę amty, jeśli nie znaleziono żadnych terminów. Pamiętaj, aby sprawdzić te zdarzenia w taki sam sposób, jak to robiszget_terms
-
Jeśli
term_to_end
parametr jest ustawiony,fields
parametru nie można ustawić na nic innego niżall
(co jest ustawieniem domyślnym). Każda inna wartość spowoduje przerwanie funkcji. Możesz to bardzo łatwo zmodyfikować, aby działało poprawnie, wbudowując pewne kontrole kodowe i zmieniając plikforeach
w pierwszym bloku kodu lub zmiana użyciaarray_search
w drugim bloku kodu -
W razie potrzeby możesz bardzo łatwo zmienić tę funkcję, aby przenieść wybrany termin na początek. Możesz także dostosować tę funkcję, aby akceptować wiele warunków do przeniesienia
-
Aby uzyskać inne informacje dotyczące korzystania z tej funkcji, zobacz
get_terms
EDYTOWAĆ
Ten kod jest teraz przetestowany i działa zgodnie z oczekiwaniami.