Custom Fields
Re-Envisioned
Support MB Custom Taxonomy Traverse custom Taxonomy Reply To: Traverse custom Taxonomy

#12435

Anh Tran
Keymaster

Hi Soren,

You can create a function to get all terms and display its custom fields like this:

function prefix_show_blogwriters() {
    $terms = wp_get_post_terms( get_the_ID(), 'blogwriter' );
    if ( empty( $terms ) ) {
        return '';
    }
    $output = '<div class="blogwriters">';
    foreach ( $terms as $term ) {
        $output .= '<div class="blogwriter">';
        $image = rwmb_meta( 'image', ['size' => 'thumbnail', 'object_type' => 'term'], $term->term_id );
        if ( $image ) {
            $output .= '<img src="' . $image['url'] . '">';
        }
        $bio = rwmb_meta( 'bio', '', $term->term_id );
        $output .= '<p>' . $bio . '</p>';
        $output .= '</div>';
    }
    $output .= '</div>';
    return $output;
}

Then you can call the function directly in your theme to output the blog writers, or output them automatically via the_content filter like this:

add_filter( 'the_content', function( $content ) {
    return $content . prefix_show_blogwriters();
} );

Or use a shortcode like this:

add_shortcode( 'blogwriters', 'prefix_show_blogwriters' );