0

working on a journal website, having custom post type "publications" with custom taxonomy "issue". In the custom taxonomy admin enter first the volume number as parent and then the issue numbers as child terms of the parent volume terms. I use this code to display a list of all issue terms but without their parent terms:

$taxonomyName = "age";
$parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => false));   
echo '<ul>';
foreach ($parent_terms as $pterm) {
    $terms = get_terms($taxonomyName, array('parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false));
    foreach ($terms as $term) {
        echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';  
    }
}
echo '</ul>';

this code displays a list like this:

  • issue 1
  • issue 2 ...

What I need exactly is getting the parent terms with the child terms to display the list as:

  • vol 1, issue 1
  • vol 1, issue 2

Any help please?

0

Have just tweaked your code to achieve what you need. See, if this works for you -

$taxonomyName = "age";
$parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => false));   
echo '<ul>';
foreach ($parent_terms as $pterm) {
    $terms = get_terms($taxonomyName, array('parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false));
    foreach ($terms as $term) {
        echo '<li>'.$pterm->name.', <a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';  
    }
}
echo '</ul>';

Specifically, I have added $pterm->name within the <li> tag which should give you parent term name along with each of its child term.

Please note: I have not tested it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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