Ask your WordPress questions! Pay money and get answers fast! (more info)

Display Parent Term of Current Child Term

  • SOLVED

I have several parent/child terms

I have pages displaying my taxonomy child terms (not parents) and I would like to show the parent of each taxonomy term as a link next to the child term name.

Can't figure out a method to accomplish this.

I've tried these, but so far no luck.

<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( '100gridgroups' ) );
$parent = get_term_by( 'id', $term->parent, get_query_var( '100gridgroups' ) );
if($parent):
echo $parent->name;
endif;
?>


<?php the_terms( '100gridgroups', array( 'parent' => 0 ) );?>

CLICK HERE TO SEE TEMPLATE CODE: [[LINK href="http://tny.cz/788696e4"]]http://tny.cz/788696e4[[/LINK]]

PASSWORD: VIEW

Answers (3)

2014-12-18

Hariprasad answers:

Hello,

Can you show the complete template file?

Thanks.


streetfire comments:

Hi there, see link in original post


Hariprasad comments:

Try this,

$term = get_term_by( 'slug', $lc->slug, '100gridgroups' );
$parent = get_term_by( 'id', $term->parent, '100gridgroups' );
if($parent):
echo $parent->name;
endif;


Hariprasad comments:

May be this is enough i think

$parent = get_term_by( 'id', $lc->parent, '100gridgroups' );
if($parent):
echo $parent->name;
endif;


streetfire comments:

Your first solution works :) :) :) Second one did not.


Hariprasad comments:

I was just checking, mon_get_alpha_terms() function outputs something else. I thought it outputs term details.


streetfire comments:

Yeah, mon_get_alpha_terms() is some crazy term sorting function. Thanks for the help!

2014-12-18

John Cotton answers:

Assuming that 'term' is a valid query var, your first block of code looks OK to me except for one thing.

Shouldn't the taxonomy param in each line just be '100gridgroups' instead of get_query_var( '100gridgroups' )?


streetfire comments:

Hum, perhaps... but removing it didn't make the terms show up.

I'm not sure if I should use term or $lc.

I'm going to post my template code above.


John Cotton comments:

$lc.

You really need to post all your code if you are having problems - we can't guess at your variable names!

2014-12-18

Tache Madalin answers:

Try this:


$term_id = $term->term_id;

$child_term = get_term( $term_id, 'category' );
$parent_term = get_term( $child_term->parent, 'category' );


And make sure you replace your category with your current taxonomy (if it's different)


streetfire comments:

$term_id = $term->term_id;

$child_term = get_term( $term_id, '100gridgroups' );

$parent_term = get_term( $child_term->parent, '100gridgroups' );


No worky :(


Tache Madalin comments:

Can you please do this?:


echo "<pre>";
print_r($term);
echo '---';
print_r($term_id);
echo '--';
print_r($child_term);
echo '--';
print_r($parent_term);
echo "</pre>";


And paste the output please ?

Thanks.