Skip to main content

Get Post Terms

PropertyValue
descriptionGet a post's terms as plain text, linked lists, or span lists.
tagslib, php, wp, oop
rating

Overview

Get a post's terms as plain text or simple HTML. The helper is useful in Bricks {echo:...} calls and regular templates when the output needs to be reusable across taxonomies.

The unified helper accepts a post ID, taxonomy, output format, term attribute, optional list item class, and plain-text separator. If the post ID is blank, it uses the current post.

Supported formats:

  • plain: News, Updates
  • links: <ul> with linked term items.
  • spans: <ul> with term labels in spans.

Supported attributes:

  • name
  • slug
  • term_id

Variants

<?php
/**
* Unified post terms helper.
*
* Formats:
* plain -> News, Updates
* links -> ul > li > a
* spans -> ul > li > span
*
* Attr:
* name | slug | term_id
*
* Args order:
* post_id, taxonomy, format, attr, class, sep
*
* @param int|string|null $post_id Post ID. Defaults to the current post.
* @param string $taxonomy Taxonomy slug.
* @param string $format Output format: plain, links, or spans.
* @param string $attr Term attribute: name, slug, or term_id.
* @param string $class Optional list item class for HTML formats.
* @param string $sep Separator for plain output.
* @return string
*/

if ( ! function_exists( 'mac_get_post_terms' ) ) {

function mac_get_post_terms(
int|string|null $post_id = null,
string $taxonomy = 'category',
string $format = 'plain',
string $attr = 'name',
string $class = '',
string $sep = ', '
): string {

$post_id = $post_id !== null && $post_id !== '' ? $post_id : get_the_ID();

if ( is_numeric( $post_id ) ) {
$post_id = (int) $post_id;
}

if ( ! is_int( $post_id ) || $post_id <= 0 || $taxonomy === '' ) {
return '';
}

$terms = get_the_terms( $post_id, $taxonomy );

if ( empty( $terms ) || is_wp_error( $terms ) ) {
return '';
}

$format = strtolower( trim( $format ) );
$attr = strtolower( trim( $attr ) );

if ( ! in_array( $format, [ 'plain', 'links', 'spans' ], true ) ) {
$format = 'plain';
}

if ( ! in_array( $attr, [ 'name', 'slug', 'term_id' ], true ) ) {
$attr = 'name';
}

$items = [];

foreach ( $terms as $term ) {

if ( ! $term instanceof WP_Term ) {
continue;
}

// Resolve value
$value = ( 'term_id' === $attr )
? (string) $term->term_id
: ( isset( $term->{$attr} ) ? (string) $term->{$attr} : '' );

if ( $value === '' ) {
continue;
}

// -------- PLAIN --------
if ( 'plain' === $format ) {

$items[] = $value;
continue;
}

// Prepare class attr (li only)
$li_class = $class ? ' class="' . esc_attr( $class ) . '"' : '';

// -------- LINKS --------
if ( 'links' === $format ) {

$url = get_term_link( $term );

if ( is_wp_error( $url ) || ! is_string( $url ) || $url === '' ) {
continue;
}

$items[] = sprintf(
'<li%s><a href="%s" rel="tag">%s</a></li>',
$li_class,
esc_url( $url ),
esc_html( $value )
);

continue;
}

// -------- SPANS --------
if ( 'spans' === $format ) {

$items[] = sprintf(
'<li%s><span>%s</span></li>',
$li_class,
esc_html( $value )
);

continue;
}
}

// -------- OUTPUT --------

if ( 'plain' === $format ) {
return implode( $sep, $items );
}

if ( empty( $items ) ) {
return '';
}

return '<ul>' . implode( '', $items ) . '</ul>';
}
}