Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

Micemade commented Sep 17, 2017

Thanks for sharing this - very usefull :)

@toongeeprime
Copy link

toongeeprime commented Jun 21, 2022

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