-
Anonymous
Hi!
I have created a third-level child term in my hierarchy on the page below; it goes Method (parent): Hand-painting (sub-parent): Professional (child). I’ve already got a script set up to hide the first parent term; I’m wondering if there would be a way to get the child (professional in this case) to show up in parentheses after its parent; so in the example link below, it would be Hand-painting (Professional), Metal under the “Material/Method”?Thanks so much,
Elisabeth -
Hi there,
How did you hide the parent term?
-
Anonymous
I’ll include the code below, Ying. It’s in the child theme functions file.
/* adding a function to hide the "other tag" of "incomplete", and other taxonomy parent categories ----------------------------------------------------------------------------*/ // Exclude internal tags from "Other tags" taxonomy add_filter( 'term_links-sign-tag', function( $term_links ) { $exclude = array( 'incomplete', 'featured-vernacular', 'featured-home', 'favorite-kvernen', 'favorite-neukirchen' ); return array_filter( $term_links, function( $link ) use ( $exclude ) { foreach ( $exclude as $tag ) { if ( stripos( $link, $tag ) !== false ) return false; } return true; } ); } ); // Exclude parent "other" terms from "Other tags" taxonomy add_filter( 'term_links-sign-tag', function( $term_links ) { $exclude = array( 'hidden', 'unique-features', 'topics' ); return array_filter( $term_links, function( $link ) use ( $exclude ) { foreach ( $exclude as $slug ) { if ( preg_match( '#/sign-tag/' . preg_quote( $slug, '#' ) . '/"#', $link ) ) { return false; } } return true; } ); } ); // Exclude parent venue terms from "Venue" taxonomy add_filter( 'term_links-venue', function( $term_links ) { $exclude = array( 'civic-institutional', 'commercial', 'culture-leisure', 'hospitality', 'residential-community' ); return array_filter( $term_links, function( $link ) use ( $exclude ) { foreach ( $exclude as $slug ) { if ( preg_match( '#/venue/' . preg_quote( $slug, '#' ) . '/"#', $link ) ) { return false; } } return true; } ); } ); // Exclude parent lettering style terms from "Lettering Style" taxonomy add_filter( 'term_links-lettering-style', function( $term_links ) { $exclude = array( 'descriptive', 'formal' ); return array_filter( $term_links, function( $link ) use ( $exclude ) { foreach ( $exclude as $slug ) { if ( preg_match( '#/lettering-style/' . preg_quote( $slug, '#' ) . '/"#', $link ) ) { return false; } } return true; } ); } ); // Exclude parent material / method terms from "Material / Method" taxonomy add_filter( 'term_links-material', function( $term_links ) { $exclude = array( 'material', 'method' ); return array_filter( $term_links, function( $link ) use ( $exclude ) { foreach ( $exclude as $slug ) { if ( preg_match( '#/material/' . preg_quote( $slug, '#' ) . '/"#', $link ) ) { return false; } } return true; } ); } ); // Exclude parent sign type terms from "Sign Type" taxonomy add_filter( 'term_sign_type', function( $term_links ) { $exclude = array( 'independent', 'fascia', 'building', 'architectural'); return array_filter( $term_links, function( $link ) use ( $exclude ) { foreach ( $exclude as $slug ) { if ( preg_match( '#/sign_type/' . preg_quote( $slug, '#' ) . '/"#', $link ) ) { return false; } } return true; } ); } ); -
George
Hi Elisabeth,
Since you’re already filtering
term_links-materialto hide the top-level parents, you can layer a second filter on the same hook that folds the third-level child into its parent’s link — so “Professional” gets pulled into “Hand-painting” as “Hand-painting (Professional)”, and the standalone “Professional” link is removed.Add this below your existing exclusion filter (order matters):
// Fold third-level child terms into their parent's link: "Parent (Child)" add_filter( 'term_links-material', function( $term_links ) { $by_slug = array(); foreach ( $term_links as $i => $link ) { if ( preg_match( '#/material/(?:[^"]*/)?([^/"]+)/"#', $link, $m ) ) { $by_slug[ $m[1] ] = $i; } } foreach ( $term_links as $i => $link ) { if ( ! preg_match( '#/material/(?:[^"]*/)?([^/"]+)/"#', $link, $m ) ) { continue; } $term = get_term_by( 'slug', $m[1], 'material' ); if ( ! $term || is_wp_error( $term ) || ! $term->parent ) { continue; } $parent = get_term( $term->parent, 'material' ); if ( ! $parent || is_wp_error( $parent ) ) { continue; } if ( ! isset( $by_slug[ $parent->slug ] ) ) { continue; } $parent_index = $by_slug[ $parent->slug ]; $term_links[ $parent_index ] = preg_replace( '#(</a>)#', ' (' . esc_html( $term->name ) . ')$1', $term_links[ $parent_index ] ); unset( $term_links[ $i ] ); } return array_values( $term_links ); }, 20 );It only folds a child in when its parent is also showing in the list, so terms like Metal (whose parent is hidden) are left alone.
One note: as written, “(Professional)” sits inside the Hand-painting link, so clicking anywhere on “Hand-painting (Professional)” goes to the Hand-painting archive, and Professional is no longer separately clickable. If you’d rather keep Professional as its own link, or have the parenthetical be plain non-linked text, let me know and I’ll adjust.
-
Anonymous
Hi George, thank you so much, I’m looking forward to trying this! By Adding this below your existing exclusion filter, you mean put it below the code I shared earlier, right?
I’d love for Professional to have its own link (separate from hand-painting); thank you so much for your work on this code.
-
George
Hi Elisabeth,
Yes exactly — put it below the exclusion filter code you shared earlier, in the same child theme functions file.
I’ve adjusted it so Professional keeps its own link. Here’s the updated version to use instead of the one I sent before:
// Fold third-level child into parent display, keeping child independently clickable add_filter( 'term_links-material', function( $term_links ) { $by_slug = array(); foreach ( $term_links as $i => $link ) { if ( preg_match( '#/material/(?:[^"]*/)?([^/"]+)/"#', $link, $m ) ) { $by_slug[ $m[1] ] = $i; } } foreach ( $term_links as $i => $link ) { if ( ! preg_match( '#/material/(?:[^"]*/)?([^/"]+)/"#', $link, $m ) ) { continue; } $term = get_term_by( 'slug', $m[1], 'material' ); if ( ! $term || is_wp_error( $term ) || ! $term->parent ) { continue; } $parent = get_term( $term->parent, 'material' ); if ( ! $parent || is_wp_error( $parent ) ) { continue; } if ( ! isset( $by_slug[ $parent->slug ] ) ) { continue; } $parent_index = $by_slug[ $parent->slug ]; $term_links[ $parent_index ] .= ' (' . $link . ')'; unset( $term_links[ $i ] ); } return array_values( $term_links ); }, 20 );This gives you “Hand-painting (Professional)” with both words as their own separate links, and leaves terms like Metal alone. Let me know how it goes!
-
Anonymous
This works great (on the page listed below). I’m now wondering if there would be a way to include the parent term in the page element that shows each tag (see second link below), so that the method of “professional” would include the parent term as well? On this “sign taxonomies archive page” element, the title is using this dynamic tag:[archive_tax_label]: {{archive_title}}. Would there be a way to pull in the parent to the archive title part?
-
George
Hi Elisabeth,
Yes — this works, though it needs a different filter than the single-sign one, because the archive title comes from GenerateBlocks’
{{archive_title}}dynamic tag rather than WordPress’s native title. GB runs its tags through its owngenerateblocks_dynamic_tag_output filter, so that’s the hook to use.Add this to your child theme functions file:
add_filter( 'generateblocks_dynamic_tag_output', function( $output, $options ) { $tag_name = $options['tag_name'] ?? ''; if ( 'archive_title' !== $tag_name ) { return $output; } if ( ! is_tax( 'material' ) ) { return $output; } $term = get_queried_object(); if ( ! $term || is_wp_error( $term ) || empty( $term->parent ) ) { return $output; } $parent = get_term( $term->parent, 'material' ); if ( ! $parent || is_wp_error( $parent ) || empty( $parent->parent ) ) { return $output; } return esc_html( $parent->name ) . ' (' . esc_html( $term->name ) . ')'; }, 10, 2 );Your Element stays exactly as-is —
[archive_tax_label]: {{archive_title}}doesn’t need to change. This only affects what{{archive_title}}outputs, so on the Professional archive the heading will read “Material / Method: Hand-painting (Professional)”.A couple of notes:
- It’s scoped to the
materialtaxonomy and to thearchive_titletag specifically, so nothing else is affected. - I included a guard so it only folds for third-level terms (where the parent itself has a parent). A second-level term like Hand-painting’s own archive stays as just “Hand-painting”. If you’d like second-level terms to show their parent too, let me know and I’ll adjust.
- It’s scoped to the
-
Anonymous
That works perfectly. Thank you so much George!
-
George
Ok, no problem!
- You must be logged in to reply to this topic.