Home / Admin / Shortcode to get all taxonomy values of current post
Duplicate Snippet

Embed Snippet on Your Site

Shortcode to get all taxonomy values of current post

This snippet declares a shortcode to get all "Artist" taxonomy values of the current post.

<10
Code Preview
php
<?php
function display_post_artists_shortcode() {
    // Get the terms (artists) of the "artist" taxonomy for the current post
    $artists = get_the_terms(get_the_ID(), 'artist');
    // Check if artists exist
    if ($artists && !is_wp_error($artists)) {
        // Start building the output
        $output = [];
        // Loop through each artist
        foreach ($artists as $artist) {
            // Generate a link to the artist archive
            $artist_link = get_term_link($artist);
            // Append each artist to the output
            $output[] = esc_html($artist->name);
        }
		
		// Merge the artist names with a comma separator
		$output = implode(', ', $output);
        return $output;
    }
    // Return an empty string if no artists are found
    return '';
}
// Register the shortcode
add_shortcode('post_artists', 'display_post_artists_shortcode');

Comments

Add a Comment