-
Anonymous
Hi,
I have created a custom post type (signs) with several taxonomies affiliated with it (location, color, lettering style, etc). On the individual single post page for the “sign” post type, there are links for each of these different pieces of data (taxonomy terms), to that term’s archive page.Right now I have an elements set up for each of the sign taxonomy archive pages, which adds up to 10 different elements overall. The layout for each of these pages is the same; the only difference between them is
1) The top of the page says the name of the taxonomy type (for example, “location,”)
2) There is a definition below the title at the top of the page that defines that taxonomy, for example, ““Location” refers to the geographic place (city, country) where the sign is found.”Would there be a way to set up ONE element that would work for all these different custom post type taxonomies, with dynamic data feeding in those two variable elements listed above?
I’m including a screenshot of the dashboard page for the “location” archive page element.
Thanks!
Elisabeth -
Hi Elisabeth,
Yes, I think it’s doable.
1. Use this code to generate a shortcode
[archive_tax_label]that shows the taxonomy name:add_shortcode( 'archive_tax_label', function() { if ( ! is_tax() && ! is_category() && ! is_tag() ) { return ''; } $queried_object = get_queried_object(); $taxonomy = get_taxonomy( $queried_object->taxonomy ); if ( $taxonomy ) { return esc_html( $taxonomy->labels->singular_name ); } return ''; });2. Here’s the code to generate a shortcode
[tax_definition]to show different text on different tax archives:/** * Shortcode: [tax_definition] * Logic: Outputs custom text based on the current taxonomy archive. */ add_shortcode( 'tax_definition', function() { // 1. Define your map: 'taxonomy_slug' => 'Your Custom Text' $definitions = array( 'location' => '“Location” refers to the geographic place (city, country) where the sign is found.', 'color' => 'The definition of color', 'resource' => 'The definition of resource', 'category' => 'The definition of category', ); // 2. Detect the current custom taxonomy foreach ( $definitions as $tax_slug => $text ) { if ( is_tax( $tax_slug ) ) { return esc_html( $text ); } } return ''; // Return nothing if no match is found }); -
Anonymous
That worked great, thank you so much!
-
No Problem 🙂
- You must be logged in to reply to this topic.