Skip to content

Instantly share code, notes, and snippets.

@ocean90
Created September 15, 2011 13:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ocean90/1219233 to your computer and use it in GitHub Desktop.
Save ocean90/1219233 to your computer and use it in GitHub Desktop.
WordPress: Check if taxonomy is assigned to a post type
<?php
if ( ds_is_taxonomy_assigned_to_post_type( 'schnipsel' ) )
echo '<h3>Schnipsel Archiv</h3>';
else
echo '<h3>Archiv</h3>';
<?php
/**
* Prüft, ob eine gegebene oder aktuell nachgefragte Taxonomy einem gegebenen Post Type zugeordnet ist.
*
* @author Dominik Schilling
* @license GPLv2
* @link http://wpgrafie.de/137/
*
* @version 0.1
* @param object|string $post_type
* @param string $taxonomy Optional. Standardwert ist null.
* @return bool True wenn Taxonomy dem Post Type zugeordnet ist, false wenn nicht und bei fehlerhafter Eingabe.
*/
function ds_is_taxonomy_assigned_to_post_type( $post_type, $taxonomy = null ) {
if ( is_object( $post_type ) )
$post_type = $post_type->post_type;
if ( empty( $post_type ) )
return false;
$taxonomies = get_object_taxonomies( $post_type );
if ( empty( $taxonomy ) )
$taxonomy = get_query_var( 'taxonomy' );
return in_array( $taxonomy, $taxonomies );
}
@Micemade
Copy link

Thanks for sharing this - very usefull :)

@toongeeprime
Copy link

This is 11years old and it helped me today... Thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment