Support » Plugins » Hacks » Getting link to child taxonomy for each post in loop

  • Morgan Kay


    This is a big complicated question, and I hope I can explain it clearly.

    Here’s what I’m trying to do: I have a custom post type (portfolio) and a custom taxonomy (genre): both are hierarchical. Some portfolios have two genres: a parent genre (for example, “fine art”) and a child genre (for example, “painting”, where “painting” is a child of “fine art”); other portfolios just have a child genre. Right now I have an archive page where people can see all portfolios in a given parent genre. I want them to be able to click on a thumbnail of each portfolio, and go to all of the portfolios in the same child genre. (So, for example, there will be one portfolio that is in both “fine art” and “painting” and many portfolios that are just in “painting”. Clicking on the one portfolio that is in both “fine art” and “painting” will take you to a page showing all portfolios that are in “painting”.)

    So each portfolio in my loop has two genres, one of which is a parent and one of which is a child. I want to provide a link to the archive for the child genre. Is there a good way to do this?

    I can do several things that come close, but none of them quite do what I need.

    get_the_term_list is a good start, but it provides links to both the parent and child genres, so that doesn’t help.

    get_terms seems more promising because it has more arguments, but as far as I can tell, it just gets all taxonomy terms, not just the terms associated with the current post in the loop.

    I don’t know if it helps, but here is my current code:

    <?php get_header(); ?>
    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ), $term->name); ?>
    <h2 class="genre-title">
            <?php printf( __( '%s', 'comicpress' ), '<span>' . $term->name . '</span>' ); ?>
    <span class="last-updated">Last updated: <?php the_time('F j, Y'); ?> </span>
    <div id="genre-content">
    <ul id="mycarousel" class="jcarousel-skin-tango">
    <?php /* This is the beginning of the loop */
    $myposts = get_posts('post_type=portfolio&post_parent=0&posts_per_page=-1&&'.$term->taxonomy.'='.$term->slug);
    foreach($myposts as $post) : setup_postdata($post); ?>
            <li class="portfolio-item">
                    <?php the_post_thumbnail('thumbnail'); /* Eventually, the thumbnail will be the link to the child genre */ ?>
            <div class="entry-content">
            <h3 class="entry-title"><?php the_title(); ?></h3>
            <?php the_content(); ?>
            <?php the_meta(); ?>
            <?php echo get_the_term_list( $post->ID, 'genre', '<p>Genre: ', ', ', '</p>' );  /*This works fine, but it gives me all genres, whereas I need a link to just the child genre */ ?>
    <?php /* This is an experiment with get_terms: the problem is that it gets all terms, not just the terms associated with the current post */
    $args = array (
            'orderby' => 'name',
            'hide_empty' => '0',
            'child_of' => '8',
    $genres = get_terms( 'genre', $args );
    echo "<pre>";
    print_r ($genres);
    echo "</pre>";
    <br /><br />
    Or there's this option:
    <?php /* This is nice, but all it gives me is the names of the current post's genres, when I need a link to the child genre */
    $args = array (
            'fields' => 'names'
    $currentgenres = wp_get_object_terms($post->ID, 'genre', $args );
    print_r ($currentgenres);
    <br /><br />
    Does this help?
    <?php $terms = get_terms('genre'); /* This isn't currently working, but when it did work, it gave me all genres, not just the child */
    echo '<ul>';
    foreach ($terms as $term) {
        echo '<li><a href="'.get_term_link($term->slug, 'genre').'">'.$term->name.'</a></li>';
    echo '</ul>';
            </div><!-- .entry-content -->
            </li><!-- .portfolio-item -->
    <?php endforeach;?>
    </div><!-- #genre-content -->
    <?php get_footer(); ?>

    You can see the results of this at although don’t be surprised if I’m messing with the page and breaking it.

    Does anyone have any suggestions here? Is there another way to do this that I’m not thinking of?

    Thank you!!

Viewing 1 replies (of 1 total)
  • Hey, sorry I don’t have an answer, but I am trying to do something almost identical to this.

    I wonder whether you ever found a solution?

Viewing 1 replies (of 1 total)
  • The topic ‘Getting link to child taxonomy for each post in loop’ is closed to new replies.