Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Help with conditional taxonomy WordPress

  • SOLVED

I have registered a taxonomy "designers" => will give
http://website.com/designers

and added the tags "John","Alex" => will give
http://website.com/designers/john
http://website.com/designers/alex

what I need is this ,

when the taxonomy tag "john" is displayed I want to show info like this
These were created by John + information


when the taxonomy tag "alex" is displayed I want to show a description like this
These were created by alex + information

sorry for the bad English..





Answers (3)

2010-09-09

enodekciw answers:

<strong>EDITED</strong>
try using has_tag('tag') conditional statement.
for example:


<?php if(has_tag('john') : ?>
<p>This text should be visible in posts with tag 'john'</p>
<?php endif; ?>
<?php if(has_tag('alex') : ?>
<p>This text should be visible in posts with tag 'alex'</p>
<?php endif; ?>


Asker comments:

it gives an error when the tag archive is viewed

http://localhost/wp/dfreebies/designer/john/

here the "designer" is the registered taxonomy and the "john" is the tag of the "designer" taxonomy.

any idea?


enodekciw comments:

Ok, that thing 'John' and 'Alex' is called term, not the tag ;)

Try this snippet:

$terms = wp_get_post_terms($post->ID, 'designers');
if($terms) {
foreach($terms as $term) {
if($term->name == 'john') {
echo 'Hello, this is John speaking';
} elseif ($term->name == 'alex') {
echo 'Hello, this is Alex speaking';
} else {
echo 'Hello, I have no term';
}
}
}

2010-09-10

Mark Duncan answers:

I think you're all making this way more complicated then it needs to be.

Firstly take a look at the WordPress template hierarchy.
http://codex.wordpress.org/images/1/18/Template_Hierarchy.png

You'll notice taxonomies and terms both support theme templates.

My suggestion, open up your theme's folder, locate the <strong>tag.php</strong> file, create a copy of said file and rename it to <strong>taxonomy-designers.php</strong>, now open that file and remove the tag heading ("Archive for tag, blah blah blah" or whatever it has as the archive heading).

Replace the previous heading with your own using something like... (based on TwentyTen theme)
<h1 class="page-title"><?php printf( __( 'These entries were created by: %s', 'twentyten' ), '<span>' . get_query_var('designers') . '</span>' ); ?></h1>

Job done.. ;)

Taxonomy term archives using the built-in templating system..


Asker comments:

Your answer had lead me to a WP tut on another site and it given me the solution.
anyway Thanks :)

2010-09-09

Tobias Nyholm answers:

This should easily be done with tags and tags descriptions.
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
if($tag->name =="something")
echo tag_description();
}
}


The tag description is set in www.example.com/wp-admin/edit-tags.php?taxonomy=post_tag

References:
http://codex.wordpress.org/Function_Reference/tag_description
http://codex.wordpress.org/Function_Reference/get_the_tags


Asker comments:

Tobias,
not only the description
I want to display any thing when the particular taxonomy tag is viewed.


Tobias Nyholm comments:

Okej....


$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
if($tag->name =="something")
echo "Write anything...";
}
}