WordPress.org

Support

Support » How-To and Troubleshooting » Custom Taxonomies conditional tags

Custom Taxonomies conditional tags

  • Hi,

    I’ve recently discovered Custom Taxonomies in WordPress. (Check out the full guide here – http://justintadlock.com/archives/2009/05/06/custom-taxonomies-in-wordpress-28 )

    The possibilities are really quite exciting but I really need to be able to make conditional tags for it to be *really* useful.

    So can anyone help me with creating custom conditional tags?
    For example before I would use if has_tags('Tom Cruise') to check if if the post was tagged with ‘Tom Cruise’ but if I had a custom taxonomy for ‘People’ then the custom conditional would need to be something like if has_people('Tom Cruise').

    I think I edit wp-includes/category-template.php to create the custom conditional tags but that’s as far as I’ve got.

    If anyone has any guidance it would be hugely appreciated.

Viewing 12 replies - 1 through 12 (of 12 total)
  • Why not use <?php if (query_posts( array( 'people' => 'Tom Cruise', 'showposts' => -1 ) ) ); ?>

    Thanks for the link to that article, btw. 🙂

    Hi Esmi,

    Thanks very much for the reply. Check out the other guides etc on that site, it’s really awesome.

    What I really want is to have in single.php something like this…

    if has_people('Tom Cruise')) {echo 'some text';}
    else if has people('Another Actor'){ echo 'some different text';}

    etc

    So your code wouldn’t be able to do that… or can you think of a way to use what you have suggested to do the same thing?

    Thanks 🙂

    Would this work?

    <?php
    $temp_query = clone $wp_query; // preserve the native query
    
    $actors = array(
    	'Tom Cruise' => 'some text',
    	'Fred Blogs' => 'some different text'
    );
    
    foreach($actors as $name=>$msg) {
    	$test = DoThe Query($name,$post->ID);
    	if($test == $name) {
    		echo $msg;
    		break 2;
    	}
    }
    
    $wp_query = clone $temp_query; // resume the native query
    
    function DoThe Query($actor,$pid) {
    	$success = '';
    	query_posts( array( 'people' => $actor, 'p' =>$pid ) ) ;
    	if (have_posts()) $success = $actor;
    	return $success;
    }
    
    ?>
    Justin Tadlock

    @greenshady

    WordPress God

    Assuming your taxonomy is person.

    Add to your theme’s functions.php file:

    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;
    }

    Call it up in your files like so:

    <?php if ( has_person( 'tom-cruise' ) ) /* do something */ ?>

    Oh nice! Must find an excuse to use that.

    Esmi thanks for yr suggestion but tried greenshady’s solution first and worked a treat.

    Thanks very much greenshady, works perfectly!

    Can I use this to use different template for different tags?

    Funny…Greenshady IS Justin Tadlock !

    how about to check if it has tag or not? like has_tag function did.

    i think i figured out the answer. i use justin function and edit some code that will do.

    function has_product_tag( $product_tag, $_post = null ) {
    	if ( !empty( $product_tag ) )
    		return false;
    
    	if ( $_post )
    		$_post = get_post( $_post );
    	else
    		$_post =& $GLOBALS['post'];
    
    	if ( !$_post )
    		return false;
    
    	$r = is_object_in_term( $_post->ID, 'product_tag', $product_tag );
    
    	if ( is_wp_error( $r ) )
    		return false;
    
    	return $r;
    }
    <?php if(has_product_tag(null)) { ?>
    // some code here
    <?php } ?>

    Thank you for everyone who contributed to this thread, it has really helped my understanding of taxonomies a great deal!

    However, I’m stuck somewhere else but near by: I have a hierarchical taxonomy ‘page_categories’ (similar to post categories) and would like to use conditional if-statements to display different images in my header.
    As I understand has_tags() will only work in the loop. I therefore would need something like ‘in_category’ or ‘is_tag’ .. I tried to modify Justin’s function, but I didn’t quite arrive where I wanted.

    Any help would be greatly appreciated!

    Thank you for the articles.
    I have blog about reviews of game.
    This is the URL
    Will it any good for my blog?

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Custom Taxonomies conditional tags’ is closed to new replies.
Skip to toolbar