0

I'm trying to output links to custom taxonomy assigned to a single post on a single post page. I've tried other thing but get errors. This is the closest I can get. It just needs to find the terms for the current post instead of outputting all terms of a taxonomy.

<?php
    $term_id = 6;
    $taxonomy_name = 'categories';
    $termchildren = get_term_children( $term_id, $taxonomy_name );
        foreach ( $termchildren as $child ) {
        $term = get_term_by( 'id', $child, $taxonomy_name );
        echo '<a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a> ';
    }
    ?>

@Howdy_McGee I tried the following code, where 'adoption-status' is a term for the custom taxonomy 'categories'. I'm looking for children of 'adoption-status'. It doesn't output anything. No errors either.

<?php get_the_terms( $post->ID, 'adoption-status' ); ?>

The following code almost works but displays all 'categories' of the current post, not just those of the term (or sub-category) 'adoption-status'

<?php echo get_the_term_list( $post->ID, 'categories', '', '', '' );?>
11
  • I think what you're looking for is get_the_terms(): codex.wordpress.org/Function_Reference/get_the_terms
    – Michelle
    Feb 19, 2015 at 16:09
  • Yeah, I tried that already but doesn't work. Feb 19, 2015 at 16:24
  • @TroyTempleman what was the output that didn't make it work? Could you post your implementation?
    – Howdy_McGee
    Feb 19, 2015 at 16:31
  • Could you possibly edit your question with the implementation you tried? Michelle is right, get_the_terms is usually the best bet for what you need.
    – Howdy_McGee
    Feb 19, 2015 at 16:46
  • 1
    @Howdy_McGee OK. To clarify, this is for a single post page. There are categories for each post (a custom taxonomy, not built in post categories). One of the categories is "Adoption Status", which has a few "sub-categories". I want to display which one of these sub-categories is selected for each post. Feb 19, 2015 at 17:41

1 Answer 1

2

I hope I understand your question. You are looking for all terms connected to a single post, but only terms that are children to the adoption-status term

To get all the terms from a specific post, you can make use of wp_get_post_terms(). This will return all the terms of a specific taxonomy connected to the post.

You will also need to make use of get_term_children() to get all the child terms from your adoption-status term

Once you have all that, it is simply to compare the two, and only returning the terms that are associated with the post and is a child term to your given term.

You can try something like the following (NOTE: Change the term ids and taxonomy names accordingly)

$child_terms = get_term_children(21, 'category');
$all_terms   = wp_get_post_terms($post->ID, 'category');

foreach ( $all_terms as $term ) {

    if( !in_array($term->term_id, $child_terms ) ) 
        continue;

    echo $term->name;

}
9
  • I tried changing the term id and taxonomy name to $child_terms = get_term_children(6, 'categories'); but get an error. Feb 19, 2015 at 17:48
  • What error. You should change taxonomy in wp_get_post_terms as well. Most probably because the taxonomy don't exist Feb 19, 2015 at 17:59
  • Pieter, I changed that too 'categories' also but get the error "Parse error: syntax error, unexpected T_ENDIF"? Feb 19, 2015 at 18:04
  • My bad, I had an extra if statement left in there by mistake. I take it out and get nothing, and no error. Feb 19, 2015 at 18:07
  • Recheck your code and ID's. On my installation, I have a child term and a grandchild term which belongs to term 21. With this code, only the child term and grandchild term are retrieved Feb 19, 2015 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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