Wie bekomme ich einen Taxonomiebegriffsnamen durch die Schnecke?
-
-
Willst dueinen Link,einen Titel,erstellen?are you wanting to create a link, title, ???
- 0
- 2011-05-05
- xLRDxREVENGEx
-
3 Antworten
- Stimmen
-
- 2011-05-05
Diegesuchte Funktionist
get_term_by
.Sie würdenes als solches verwenden:<?php $term = get_term_by('slug', 'my-term-slug', 'category'); $name = $term->name; ?>
Diesführt dazu,dass
$term
ein Objektist,das Folgendesenthält:term_id name slug term_group term_taxonomy_id taxonomy description parent count
Der Codexerklärt diese Funktion hervorragend: http://codex.wordpress.org/Function_Reference/get_term_by
The function you are looking for is
get_term_by
. You would use it as such:<?php $term = get_term_by('slug', 'my-term-slug', 'category'); $name = $term->name; ?>
This results in
$term
being an object containing the following:term_id name slug term_group term_taxonomy_id taxonomy description parent count
The codex does a great job explaining this function: http://codex.wordpress.org/Function_Reference/get_term_by
-
Du warst schneller alsich.Genau das würdeichtun.you beat me to it. This is exactly what i would do to.
- 0
- 2011-05-05
- xLRDxREVENGEx
-
Wasist,wenn Sienicht über die Taxonomie verfügen?What if you don't have the taxonomy slug?
- 1
- 2017-05-07
- EkoJR
-
Sie können `get_term ($term_id);` verwenden,wenn Sienur die ID haben.You can use `get_term( $term_id );` if you only have the ID.
- 0
- 2020-07-11
- Gavin
-
- 2017-05-07
Diesgibt eine Antwort,wenn die Taxonomienicht verfügbar/unbekannt ist.
Inmeinem Fallgabesbei Verwendung von get_term_by einige Fälle,in denen dies der Fall war warnur der Term Slug (keine Term ID oder Taxonomie). Wasmich hierhergeführt hat. Die Antwort hatmein Problemjedochnichtganzgelöst.
Lösungfür leere
$taxonomy
// We want to find the ID to this slug. $term_slug = 'foo-bar'; $taxonomies = get_taxonomies(); foreach ( $taxonomies as $tax_type_key => $taxonomy ) { // If term object is returned, break out of loop. (Returns false if there's no object) if ( $term_object = get_term_by( 'slug', $term_slug , $taxonomy ) ) { break; } } $term_id = $term_object->name; echo 'The Term ID is: ' . $term_id . '<br>'; var_dump( $term_object );
Ergebnis
The Term ID is: 32 object(WP_Term) public 'term_id' => int 32 public 'name' => string 'Example Term' public 'slug' => string 'example-term' public 'term_group' => int 0 public 'term_taxonomy_id' => int 123 public 'taxonomy' => string 'category' public 'description' => string '' public 'parent' => int 0 public 'count' => int 23 public 'filter' => string 'raw'
Wiefolgterhält das Konzeptein Array von
$taxonomies
,durchläuft das Array und wennget_term_by()
eine Übereinstimmung zurückgibt,brichtes sofort aus dem ausfürjede Schleife.Hinweis: Ich habe versucht,nacheiner Methode zu suchen,um die zugehörige Taxonomie (ID oder Slug) von Term Slug abzurufen. Leider kannichin WordPressnichtsfinden.
This provides an answer when the taxonomy is unavailable/unknown.
In my case, when using get_term_by, there were some instances where there was only the Term Slug ( No Term ID or Taxonomy ). Which led me here. However, the answer provided didn't quite resolve my issue.
Solution for empty
$taxonomy
// We want to find the ID to this slug. $term_slug = 'foo-bar'; $taxonomies = get_taxonomies(); foreach ( $taxonomies as $tax_type_key => $taxonomy ) { // If term object is returned, break out of loop. (Returns false if there's no object) if ( $term_object = get_term_by( 'slug', $term_slug , $taxonomy ) ) { break; } } $term_id = $term_object->name; echo 'The Term ID is: ' . $term_id . '<br>'; var_dump( $term_object );
Result
The Term ID is: 32 object(WP_Term) public 'term_id' => int 32 public 'name' => string 'Example Term' public 'slug' => string 'example-term' public 'term_group' => int 0 public 'term_taxonomy_id' => int 123 public 'taxonomy' => string 'category' public 'description' => string '' public 'parent' => int 0 public 'count' => int 23 public 'filter' => string 'raw'
As follows, the concept gets an array of
$taxonomies
, loops through the array, and IFget_term_by()
returns a match, it then immediately breaks out of the foreach loop.Note: I tried searching for a method to get the associated taxonomy ( ID or Slug ) from Term Slug, but unfortunately I am unable to find anything available in WordPress.
-
- 2019-01-03
danke,das hatbei mirfunktioniert.
Ich habeeine Funktionerstellt undbenutze siebei Bedarfimmer wieder.
function helper_get_taxonomy__by_slug($term_slug){ $term_object = ""; $taxonomies = get_taxonomies(); foreach ($taxonomies as $tax_type_key => $taxonomy) { // If term object is returned, break out of loop. (Returns false if there's no object); if ($term_object = get_term_by('slug', $term_slug, $taxonomy)) { break; }else{ $term_object = "Warn! Helper taxonomy not found."; } } return $term_object; }
thanks, this worked for me.
I created a function and use it again and again as needed.
function helper_get_taxonomy__by_slug($term_slug){ $term_object = ""; $taxonomies = get_taxonomies(); foreach ($taxonomies as $tax_type_key => $taxonomy) { // If term object is returned, break out of loop. (Returns false if there's no object); if ($term_object = get_term_by('slug', $term_slug, $taxonomy)) { break; }else{ $term_object = "Warn! Helper taxonomy not found."; } } return $term_object; }
-
Sie solltenbei Erfolg dieselben Typen wieget_term_by zurückgeben: (WP_Term| array| false) WP_Term-Instanz (oder -Array).Gibtfalse zurück,wenn $taxonomynicht vorhandenist oder $termnichtgefunden wurde.You should return the same types as get_term_by: (WP_Term|array|false) WP_Term instance (or array) on success. Will return false if $taxonomy does not exist or $term was not found.
- 0
- 2020-05-25
- xnagyg
Wennicheinen Taxonomiebegriff kenne,wie kannich den Namen dieses Begriffserhalten?