Custom Fields
Re-Envisioned
Support MB Custom Taxonomy Traverse custom Taxonomy

This topic contains 1 reply, has 2 voices, and was last updated by  Anh Tran 2 months, 2 weeks ago.

  • Creator
    Topic
  • #12402

    Soren Munk
    Participant

    I have a custom taxonomy called Blogwriter that is assigned to regular Posts (to help assigning more than one writer to a blog entry). The taxonomy has a few fields, Featured_image and Biography among others.

    Now, at the end of each post I would like to show the image and the biography for each blogwriter assigned to the specific Post, is there some shortcode tags I can use to do that? (or some other way).

    That is, traverse a custom taxonomy at display some custom fields for each assigned taxonomy item.

    Thank you,
    Soren.

Viewing 1 replies (of 1 total)
  • Author
    Replies
  • #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' );

Viewing 1 replies (of 1 total)

You must be logged in to reply to this topic.