Assuming the name of the custom taxonomy is like_a_tag
:
Using a Pods Single Item
block, input this Custom Template
:
<div>
[each like_a_tag]
<span>{@name}</span>
[/each]
</div>
Alternatively, with links to the taxonomy archives:
<div>
[each like_a_tag]
<span><a href="{@permalink}">{@name}</a></span>
[/each]
</div>
Alternatively, as a shortcode which could be used in a Paragraph
block or similar content blocks, a Shortcode
block, or within a Pods Custom Template
if define( 'PODS_SHORTCODE_ALLOW_SUB_SHORTCODES', true );
is added to wp-config.php
:
[pods-like-a-tag]
<?php
/**
* [pods-like-a-tag]
*
* Output linked terms from taxonomy 'like_a_tag' using a pods() object.
*/
add_shortcode(
'pods-like-a-tag',
function( $atts = [], $content = '', $tagname = '' ) {
// Causes output to be stored to a variable.
ob_start();
// pods() defaults to the current object ID and post type.
$pod = pods();
// 'like_a_tag' is the name of the taxonomy.
$terms = (array) $pod->field( 'like_a_tag' );
if ( ! empty( $terms ) ) {
// If there are terms set.
echo '<div>';
foreach( $terms as $term ) {
/**
* printf() takes a template for the first argument,
* where %s or %d are filled in with the
* following arguments in order of appearance.
*
* pods()->field() returns an array for $term.
*/
printf(
'<span><a href="%s">%s</a></span> ',
esc_url( get_term_link( (int) $term['term_id'] ) ),
esc_html( $term['name'] )
);
}
echo '</div>';
}else {
// If there are no terms set.
echo '<div>No tags.</div>';
}
// Returns stored output, as is required for a shortcode.
return ob_get_clean();
}
);
Finally, here is a similar shortcode using WordPress core functions instead of a pods() object:
[like-a-tag]
<?php
/**
* [like-a-tag]
*
* Output linked terms from taxonomy 'like_a_tag' using WordPress core function wp_get_post_terms().
*/
add_shortcode(
'like-a-tag',
function( $atts = [], $content = '', $tagname = '' ) {
// Causes output to be stored to a variable.
ob_start();
// 'like_a_tag' is the name of the taxonomy.
$terms = (array) wp_get_post_terms( get_the_ID(), 'like_a_tag' );
if ( ! empty( $terms ) ) {
// If there are terms set.
echo '<div>';
foreach( $terms as $term ) {
/**
* printf() takes a template for the first argument,
* where %s or %d are filled in with the
* following arguments in order of appearance.
*
* wp_get_post_terms() returns a WP_Term object for $term.
*/
printf(
'<span><a href="%s">%s</a></span> ',
esc_url( get_term_link( (int) $term->term_id ) ),
esc_html( $term->name )
);
}
echo '</div>';
}else {
// If there are no terms set.
echo '<div>No tags.</div>';
}
// Returns stored output, as is required for a shortcode.
return ob_get_clean();
}
);
References: