Contents
Unfortunately (in my eyes)I haven’t found a way inside a post or page to query what taxonomies have beenused to apply terms. This for me would be a bit of a holy grail.
Anyeway – these are the bits and pieces I have developed when trying to get various bits and pieces of information from taxonomies and terms applied to a page or post.
Get the current page ID
<?php $thePostID = $post->ID; echo $thePostID; ?>
Get both the Taxonomy Name and Slug
<?php // Get both the Taxonomy Name and Slug $terms = get_taxonomy( 'slide-type-location' ); $taxonomySlug = $terms->name; $taxonomyName = $terms->label; echo "<br/>"; echo $taxonomySlug; echo "<br/>"; echo $taxonomyName; ?>
Get Taxonomy Term (archive – taxonomy.php) link
<?php // Get Taxonomy Term (archive - taxonomy.php) link $taxTitle = get_the_term_list( get_the_ID(), 'slide-type-location', '', ', ', '' ); echo "<br/>"; echo $taxTitle; ?>
Get Taxonomy Term Slug
<?php // Get Taxonomy Term Slug $terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'slide-type-location', '', ', ', '' ) ); echo "<br/>"; echo $terms_as_text; ?>
Check for Terms applied from a specified Taxonomy and print info or add it variables
This code is very useful as it provides a foreach loop where you can do something for each term found.
<?php // Check for Terms applied from a specified Taxonomy and print info or add it variables $terms = get_the_terms( $post->ID , 'slide-type-location' ); // Loop over each item since it's an array foreach( $terms as $term ) { // Print the name method from $term which is an OBJECT print 'This slider is intended for the '; print $term->name; $TaxName = $term->slug; print '<br />'; print $TaxName; print '<br />'; // Get rid of the other data stored in the object, since it's not needed unset($term); } //Here we echo a variable set in the foreach loop after it has finished. // This may be useful for posts/pages where only one term is being a applied. echo $TaxName; echo "<br/>"; ?>
Check for a Taxonomy Terms and execute code
<?php <!--This code checks for taxonomy terms and allows specific code to runs if they are found.--> <?php if (is_object_in_term($post->ID,'slide-type-location','Home Page')) : ?> <!--Yes this has the term 'Home Page' - do some code here.--> <?php elseif (is_object_in_term($post->ID,'slide-type-location','News Page')) : ?> <!--Yes this has the term 'News Page' - do some code here.--> <?php else : ?> <!--None of the above terms where found - do some code here iof necessary.--> <?php endif; ?> ?>
How to create a tag cloud with your custom taxonomies
<?php wp_tag_cloud( array( 'taxonomy' => 'people', 'number' => 45 ) ); ?>
Untested: Redirect term archives to posts
<?php <pre><code>add_action( 'template_redirect', 'my_redirect_term_to_post' ); function my_redirect_term_to_post() { global $wp_query; if ( is_tax() ) { $term = $wp_query->get_queried_object(); if ( 'actor' == $term->taxonomy ) { $post_id = my_get_post_id_by_slug( $term->slug, 'person' ); if ( !empty( $post_id ) ) wp_redirect( get_permalink( $post_id ), 301 ); } } } function my_get_post_id_by_slug( $slug, $post_type ) { global $wpdb; $slug = rawurlencode( urldecode( $slug ) ); $slug = sanitize_title( basename( $slug ) ); $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = %s", $slug, $post_type ) ); if ( is_array( $post_id ) ) return $post_id[0]; elseif ( !empty( $post_id ) ); return $post_id; return false; }</code></pre> ?>
Taken from this great page on Justoin Tadlocks website:
http://justintadlock.com/archives/2010/08/20/linking-terms-to-a-specific-post
Function to check for a Taxonomy Term & do something
<pre> <pre> <pre> <pre><?php <pre><code>function has_person( $person, $_post = null ) { if ( empty( $person ) ) return false; if ( $_post ) $_post = get_post( $_post ); else $_post =& $GLOBALS['post']; if ( !$_post ) return false; $r = is_object_in_term( $_post->ID, 'person', $person ); if ( is_wp_error( $r ) ) return false; return $r; }</code></pre> </pre> </pre> ?></pre> </pre>
Revisions
There are no revisions for this post.
No comments yet.