Custom Fields
Re-Envisioned
Support MB Term Meta Show custom Fields on a list of Taxonomy terms

  • Creator
    Topic
  • #32314
    Resolved Knüt
    Participant

    Hello!

    I need to display a list of terms from a Taxonomy of a Custom Post Type with some data of a Custom Field.

    From now on I have set up the CPT (article) with the Taxonomy (butlleti) and created a bunch of terms for this Taxonomy. I also created two Custom Fields (id 'mes_butlleti' and 'any_butlleti') for that Taxonomy (butlleti).

    Then, I managed to have this code that creates the list of terms:

    <?php
    $terms = get_terms( array(
    'taxonomy' => 'butlleti', // set your taxonomy here
    'orderby' => 'date', // default: 'orderby' => 'name',
    'order' => 'DESC',
    'hide_empty' => false, // default: true
    ) );
    if ( empty( $terms ) || is_wp_error( $terms ) ) {
    return;
    }
    echo '<div class="butlletins__list-container">';
    foreach( $terms as $term ) {
    printf(
    '<a href="%s">
        <div class="butlletins__list">
            <p class="p-big">ARTICLE N. <strong>%s</strong></p>
            <div class="butlletins__list-line"></div>
            <p> HERE I NEED TO PRINT THE TWO CUSTOM FIELDS </p>
        </div>
    </a>',
    esc_url( get_term_link( $term ) ),
    esc_attr( $term->name )
    );
    }
    echo '</div>';
    ?>

    I would like to print the data from both of the Custom Fields associated to the Taxonomy (butlleti) for each term.

    Is that possible? Can you give me some light?

    Thank you very much!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #32327
    Long Nguyen
    Moderator

    Hi,

    Please follow this topic to show the term meta https://metabox.io/support/topic/taxonomy-image-on-custom-post-type/

    and read more on the documentation https://docs.metabox.io/extensions/mb-term-meta/

    #32333
    Knüt
    Participant

    Hi Long Nguyen,

    Thanks for your help!

    I've been reading your documentation, but I can't figure it out how to make it work with the code I already have to show both the Terms and a Custom Field of that term.

    Could you please help me figure it out?

    Thank you!

    #32344
    Long Nguyen
    Moderator

    Hi,

    Here is an example to show the term meta in the loop

    foreach ( $terms as $term ) {
        $mes_butlleti= rwmb_meta( 'mes_butlleti', ['object_type' => 'term'], $term->term_id );
        echo $mes_butlleti; 
    }
    #32354
    Knüt
    Participant

    Hi,

    That's great!

    Do you know how I can print $mes_butlleti and $any_butlleti into the HTML below?

    I would like to have those term data where it says <p>HERE I NEED TO PRINT THE mes_butlleti AND any_butlleti DATA<p>

    <?php
    $terms = get_terms( array(
    'taxonomy' => 'butlleti', // set your taxonomy here
    'orderby' => 'none', // default: 'orderby' => 'name',
    'order' => 'ASC',
    'hide_empty' => true, // default: true
    'number' => 1,
    ));
    if ( empty( $terms ) || is_wp_error( $terms ) ) {
    return;
    }
    echo '<div class="butlletins__list-container">';
    foreach( $terms as $term ) {
        $mes_butlleti= rwmb_meta( 'mes_butlleti', ['object_type' => 'term'], $term->term_id );
        $any_butlleti= rwmb_meta( 'any_butlleti', ['object_type' => 'term'], $term->term_id );
        echo $mes_butlleti;
        echo $any_butlleti;
    printf(
    '<a href="%s">
        <div class="butlletins__list">
            <p class="p-big">BUTLLETÍ N. <strong>%s</strong></p>
            <div class="butlletins__list-line"></div>
            <p>HERE I NEED TO PRINT THE mes_butlleti AND any_butlleti DATA<p>
            <span class="butlletins__list-new">NOU</span>
        </div>
    </a>',
    esc_url( get_term_link( $term ) ),
    esc_attr( $term->name ),
    $term->count
    );
    }
    echo '</div>';
    
    ?>

    Thanks!!!

    #32355
    Knüt
    Participant

    Hi!

    I rewrite all the code using echo, not printf, and now it works fine!

    Thanks for your help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.