To show the custom taxonomy terms of a post in WordPress, for example the project type of a portfolio item, you need the terms of the current post from one taxonomy, printed as links you can style one by one.
This snippet prints them in a wrapper, and every term gets a class from its slug, such as dpc-term--branding, so you can give each one its own badge colour. Posts without terms print nothing.
The older version ran foreach over false on posts without terms, which is a PHP warning, and printed names without escaping. Both are fixed.
The snippet
<?php
/**
* The current post's terms from one taxonomy, each with a class from its slug.
* Scripts Organizer: Trigger location Conditions, Script location Shortcode.
*/
$dpc_taxonomy = 'post_tag'; // Your taxonomy slug, e.g. 'projekte_cat'.
$dpc_link = true; // false: plain labels without links.
$dpc_terms_post_id = get_the_ID();
$dpc_terms = $dpc_terms_post_id ? get_the_terms( $dpc_terms_post_id, $dpc_taxonomy ) : false;
// get_the_terms() returns false for no terms and WP_Error for an unknown taxonomy.
if ( $dpc_terms && ! is_wp_error( $dpc_terms ) ) {
echo '<div class="dpc-terms dpc-terms--' . esc_attr( $dpc_taxonomy ) . '">';
foreach ( $dpc_terms as $dpc_term ) {
$dpc_url = $dpc_link ? get_term_link( $dpc_term ) : '';
if ( $dpc_url && ! is_wp_error( $dpc_url ) ) {
printf(
'<a class="dpc-term dpc-term--%1$s" href="%2$s" rel="tag">%3$s</a>',
esc_attr( $dpc_term->slug ),
esc_url( $dpc_url ),
esc_html( $dpc_term->name )
);
} else {
printf(
'<span class="dpc-term dpc-term--%1$s">%2$s</span>',
esc_attr( $dpc_term->slug ),
esc_html( $dpc_term->name )
);
}
}
echo '</div>';
}
Add it with Scripts Organizer
In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Conditions, tick Shortcode under Script location (leave PHP unticked), paste the code into the PHP editor, keep Template on All and publish.
The block now shows its shortcode, for example [scorg_shortcode id="123"]. Paste it where the output should appear: a Shortcode block in the post or in a block theme template, or the shortcode element of your page builder’s single or archive template.
Set $dpc_taxonomy to your taxonomy slug first. With Template → Post Type you can limit the block to the post type that uses this taxonomy. Scripts Organizer includes this file every time the shortcode renders, inside its own function. That is why the code has no function declarations (a second shortcode on the same page would fatal with “Cannot redeclare”), uses get_the_ID() or get_queried_object_id() instead of a bare $post, and prefixes its variables with dpc_.
How it works
get_the_terms()returns the terms of the current post from the object cache. It returnsfalsewhen there are none and aWP_Errorwhen the taxonomy doesn’t exist; both print nothing.- The old code called
get_term()twice for every term to read its name and slug. The term objects already have both. get_term_link()can fail for a taxonomy that is not public; such terms fall back to a plainspan. Names, slugs and URLs are escaped.
Customise it
- Labels without links: set
$dpc_linktofalse. - Badge colours:
.dpc-term{display:inline-block;padding:.2em .6em;border-radius:1em;background:#eee}and.dpc-term--branding{background:#ffe08a}. - Categories with a primary category: see Show the current post’s categories.
Gotchas
- Terms are in the order WordPress stores them, usually by name. For a custom order, sort
$dpc_termswithusort()before the loop. - Block themes have a Post Terms block that lists the terms of any taxonomy; use this snippet when you need the slug classes.
In the Scripts Organizer library Import it with one click from Scripts Organizer.