Ich habe einen benutzerdefinierten Beitragstyp "ioni_codex". Ich verwende die integrierte Wordpress-Kategorie als Taxonomie
Ich möchte alle Kategorien auflisten, die von "ioni_codex" verwendet werden.
Ich gehe davon aus, dass dieser Code den Trick macht:
$myargs = array (
"type" => "ioni_codex"
);
$categories = get_categories( $myargs );
Allerdings sehe ich stattdessen die Liste von Alle Kategorien nicht die von "ioni_codex" zugewiesenen Kategorien.
Was mache ich falsch?
Antworten:
3 für die Antwort № 1get_categories()
akzeptiert nicht post_type
Verwenden Sie als Argument taxonomy
Verwenden Sie stattdessen die Taxonomie mit dem Namen, den Sie bei der Registrierung angegeben haben. Hier ist ein Link zum Codex, der es genauer erklären kann - http://codex.wordpress.org/Function_Reference/get_categories.
0 für die Antwort № 2
Ich habe eine Antwort bei einem Schwesterprojekt: Bainternet ♦ hat die Funktion get_terms () neu geschrieben, um den post_type bereitzustellen
Bitte beziehen Sie sich auf seine Lösung Hier oder einfach von unten kopieren und einfügen:
/* get terms limited to post type
@ $taxonomies - (string|array) (required) The taxonomies to retrieve terms from.
@ $args - (string|array) all Possible Arguments of get_terms http://codex.wordpress.org/Function_Reference/get_terms
@ $post_type - (string|array) of post types to limit the terms to
@ $fields - (string) What to return (default all) accepts ID,name,all,get_terms.
if you want to use get_terms arguments then $fields must be set to "get_terms"
*/
function get_terms_by_post_type($taxonomies,$args,$post_type,$fields = "all"){
$args = array(
"post_type" => (array)$post_type,
"posts_per_page" => -1
);
$the_query = new WP_Query( $args );
$terms = array();
while ($the_query->have_posts()){
$the_query->the_post();
$curent_terms = wp_get_object_terms( $post->ID, $taxonomy);
foreach ($curent_terms as $t){
//avoid duplicates
if (!in_array($t,$terms)){
$terms[] = $c;
}
}
}
wp_reset_query();
//return array of term objects
if ($fields == "all")
return $terms;
//return array of term ID"s
if ($fields == "ID"){
foreach ($terms as $t){
$re[] = $t->term_id;
}
return $re;
}
//return array of term names
if ($fields == "name"){
foreach ($terms as $t){
$re[] = $t->name;
}
return $re;
}
// get terms with get_terms arguments
if ($fields == "get_terms"){
$terms2 = get_terms( $taxonomies, $args );
foreach ($terms as $t){
if (in_array($t,$terms2)){
$re[] = $t;
}
}
return $re;
}
}