WordPress.org

Ready to get started?Download WordPress

Forums

Custom Taxonomies + conditional tags (13 posts)

  1. huckmag
    Member
    Posted 5 years ago #

    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.

  2. esmi
    Forum Moderator
    Posted 5 years ago #

    Why not use <?php if (query_posts( array( 'people' => 'Tom Cruise', 'showposts' => -1 ) ) ); ?>

    Thanks for the link to that article, btw. :-)

  3. huckmag
    Member
    Posted 5 years ago #

    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 :)

  4. esmi
    Forum Moderator
    Posted 5 years ago #

    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;
    }
    
    ?>
  5. Justin Tadlock
    Member
    Posted 5 years ago #

    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 */ ?>
  6. esmi
    Forum Moderator
    Posted 5 years ago #

    Oh nice! Must find an excuse to use that.

  7. huckmag
    Member
    Posted 5 years ago #

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

    Thanks very much greenshady, works perfectly!

  8. Pali Madra
    Member
    Posted 4 years ago #

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

  9. josemv
    Member
    Posted 4 years ago #

    Funny...Greenshady IS Justin Tadlock !

  10. Od3n
    Member
    Posted 4 years ago #

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

  11. Od3n
    Member
    Posted 4 years ago #

    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 } ?>
  12. c.e.macht
    Member
    Posted 4 years ago #

    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!

  13. petrusfajarsubekti
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic