Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am stuck on this. I thought I had it correct but then realized it's not. I have a custom hierarchical taxonomy based on location. States are top level and counties are children. I have the top level states in a right side menu and when chosen take me to the taxonomy page for that state. Within that state page the right hand menu should display the counties for only that particular state. Instead it is retrieving all the children of the states and displaying. Since this is a taxonomy page for the state is their a query to determine the current top level page and just display the children as links? My current code is below.

<?php
$taxonomyName = "location";
//This gets top layer terms only.  This is done by setting parent to 0.  
$parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => false));   
echo '<ul>';
foreach ($parent_terms as $pterm) {
//Get the Child terms
$terms = get_terms($taxonomyName, array('parent' => $pterm->term_id,'orderby' => 'slug', 'hide_empty' => false));
foreach ($terms as $term) {
    echo '<li><h3><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</h3></a></li>';  
}
}
echo '</ul>';
?>

Thanks for any assistance.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Use get_queried_object() to get the current queried term on a category page:

$this_term = get_queried_object();
$args = array(
    'parent' => $this_term->term_id,
    'orderby' => 'slug',
    'hide_empty' => false
);
$child_terms = get_terms( $this_term->taxonomy, $args );
echo '<ul>';
foreach ($child_terms as $term) {
    echo '<li><h3><a href="' . get_term_link( $term->name, $this_term->taxonomy ) . '">' . $term->name . '</h3></a></li>';  
}
echo '</ul>';
share|improve this answer
    
Works perfectly. Thanks!! –  Deon Aug 27 '13 at 19:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.