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

If statement for custom taxonomy parent term WordPress

  • SOLVED

I've create a custom post type called "schedahotel" and have added a hierarchical taxonomy called "localita_hotel" with slug "hotel" to it.

I've added the following "localita_hotel" : "rimini", "riccione" and "bellaria-igea-marina".

I've added inside "rimini" and inside "bellaria-igea-marina" : "4-stelle", "3-stelle" and "2-stelle"

I've created a taxonomy.php page.

When i go to the url ../hotel/rimini/4-stelle i show the correct posts inside.

But i want to create an if statement inside taxonomy.php

For example this work:


<?php
$lengh_loc_desc = 330;

if ( is_tax( 'localita_hotel', 'rimini' ) ) {
echo '<h1>';
echo $sm_loc_rimini_title;
echo '</h1>';
echo '<h2>';
echo substr($sm_loc_rimini_desc,0,$lengh_loc_desc);
echo '</h2>';
} elseif ( is_tax( 'localita_hotel', 'bellaria-igea-marina' ) ) {
echo '<h1>';
echo $sm_loc_bellaria_igea_marina_title;
echo '</h1>';
echo '<h2>';
echo substr($sm_loc_bellaria_igea_marina_desc,0,$lengh_loc_desc);
echo '</h2>';
} else {
echo '<h1>';
echo $sm_loc_rimini_title;
echo '</h1>';
echo '<h2>';
echo substr($sm_loc_rimini,0,$lengh_loc_desc);
echo '</h2>';
}
?>


What is the code for "echo" the parent term?

<em>Example: if the term is RIMINI--->3-STELLE echo MY_TEXT</em>

Thanks!

Answers (3)

2011-10-04

Dylan Kuhn answers:

It sounds like you want to check the parent of the queried taxonomy. So in /hotel/rimini/4-stelle, to get the 'rimini' term:


$queried_term = get_queried_object();
$parent_term = get_term( $queried_term->parent, 'localita_hotel' );
if ( 'rimini' == $parent_term->slug )
echo 'MY TEXT';


christian pucci comments:

Nice Dylan! Thanks!

it work but with this exatly:


<?php

$queried_term = get_queried_object();
$parent_term = get_term( $queried_term->parent, 'localita_hotel' );

if ( is_tax( 'rimini', '3-stelle' ) ) {
echo '<h1>';
echo 'text_1';
echo '</h1>';
} elseif ( is_tax( 'rimini', '4-stelle' ) ) {
echo '<h1>';
echo 'text_2';
echo '</h1>';
} else {
echo '<h1>';
echo 'text_3';
echo '</h1>';
}

?>


Many many thanks!


Dylan Kuhn comments:

Your code might work without including any of mine, then, though I'm not sure I understand why. But if it's working for you I'll see if I can answer my own question now :)

2011-10-04

Kannan C answers:

you can use
$tax_data = $wp_query->get_queried_object();
This object class will give you the name and taxonomy details.

2011-10-04

Abdessamad Idrissi answers:

simple :)


if ( is_tax( 'rimini', '3-stelle' ) ) {
echo 'your text';
}