0

I've gone through other posts here, but have been unsuccessful making this work

I think I need a push at the fundamental starting point for this, as I can't seem to convert the resulting output into its adjoining label.

When looking at a Taxonomy Term Archive page, I would like to display the Top Level Taxonomy Name above the output.

With the following code, if I have a Taxonomy 'Kitchen Features', slug 'kitchen-features' - and I am on a Term Archive for this Taxonomy (i.e. Dishwasher) this code will output 'kitchen-features'. I need instead 'Kitchen Features'.

I thought I could achieve this with the addition of ->labels->name but evidently not applying it properly. So below I am showing the code to the point of returning 'kitchen-features', what do I need to do with $taxonomy to reach the Label? Thanks for any thoughts

private function get_taxonomy_by_term_id( $term_id ) {

    // We can't get a term if we don't have a term ID.
    if ( 0 === $term_id || null === $term_id ) {
        return;
    }

    // Grab the term using the ID then read the name from the associated taxonomy.
    $taxonomy = '';
    $term = get_term( $term_id );
    if ( false !== $term ) {
        $taxonomy = $term->taxonomy;
    }
    return trim( $taxonomy );
}

1 Answer 1

3

try this:

private function get_taxonomy_by_term_id( $term_id ) {

    // We can't get a term if we don't have a term ID.
    if ( 0 === $term_id || null === $term_id ) {
        return;
    }

    // Grab the term using the ID then read the name from the associated taxonomy.
    $taxName = '';
    $term = get_term( $term_id );
    if ( false !== $term ) {
        $taxonomy = $term->taxonomy;
        $taxName = get_taxonomy($taxonomy)->labels->name

    }

    return trim( $taxName );
}

source: https://codex.wordpress.org/Function_Reference/get_taxonomy

1
  • Thanks, this works. I could see from pulling query vars that I needed to address another pull for this, but wasn't able to formulate it properly. Appreciate the help.
    – busy360
    Commented Jun 30, 2017 at 19:01

Your Answer

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

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