I’ve searched but can’t find the answer but the use case with this, this time round, is that I want to display a single wp ‘tag’ term (out of many applied).
NB {post_terms_post_tag:plain} is great for showing terms (without links, which I also need) but I have content with multiple tags and only have room to display one of them.
Free ice-cream to anyone who knows. I’ll send it in the post!
Since i had to do this quite often, i created a little composer package that is open source to ease the whole process:
Potential code for your problem could look like this:
add_action('init', function() {
juvo\Bricks_Dynamic_Data_Tags\DDT_Registry::getInstance()
->set('single_post_tag', 'Single Post Tag', 'Post Tags', function($post) {
$tags = get_the_tags($post->ID);
if ($tags && !is_wp_error($tags)) {
// Get the first tag or implement your own logic to choose a tag
return '<a href="' . get_tag_link($tags[0]->term_id) . '">' . esc_html($tags[0]->name) . '</a>'; // Change index if you want a different tag
}
return '';
});
});
Thanks @JUVO. Much appreciated. Ice-cream in the post - and I’ll ask Bricks if they can add direct then too, as other systems such as crockblock/elementor have that built-in - assuming there is no other way direct Bricks way to do this - but thanks for the tip/reply.
This is super old, but for anyone looking for a non-code implementation, you could use the query builder.
Add a div/block with a query for post tags, toggle “Current post term”, limit the results to 1. Inside this div/block, add a text module and use {term_name}.
This method allows you to get more granular with the results, being able to order the terms differently to get different results. For example, you might use this method for categories and want to use the most recently created category, alphabetical, etc.
I was looking for a way to do this without having to using the query builder in case that creates too much latency, but so far it seems to be functioning pretty alright.