Custom Fields
Re-Envisioned
Support MB Custom Taxonomy Display Custom Field in Taxonomy

  • Creator
    Topic
  • #30727
    Resolved Janos
    Participant

    Hello,

    I need some light here. I made the list of categories (slug = termekkategoria) on a page. I was successfully querying the slugs and names of the categories. I also made a custom field of featured category image, connected to the taxonomy. I can choose the image on the backend on the Termekkategoria page.

    However, I cannot display the featured image of the categories on the frontend. May I get some help here? I use the following code:

    <div class="cat-card--container">
    
    <?php
    // your taxonomy name
    $tax = 'termekkategoria';
    
    // get the terms of taxonomy
    $terms = get_terms( $tax, $args = array(
      'hide_empty' => false // do not hide empty terms
    ));
    
    // loop through all terms
    foreach( $terms as $term ) {
    
    if( $term->count > 0 )
        // display link to term archive
        echo "<a class='cat-card--link' href=" . $term->slug . ">
                <div class='cat-card'>
                    <div>
                        <img class='cat-card--img' url=". $term->termekkategoria_kep ."></div>
                    <div class='cat-card--title'>". $term->name . "</div>
                </div>
              </a>";
    }
    
    ?>
        
    </div>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #30732
    Long Nguyen
    Moderator

    Hi Janos,

    To get the term meta, please follow this documentation https://docs.metabox.io/extensions/mb-term-meta/
    For example:

    foreach( $terms as $term ) {
        $image = rwmb_meta( 'single_image', ['object_type' => 'term'], $term->term_id );
        echo '<img src="' . $image['url'] . '">';
    }
    #30742
    Janos
    Participant

    Thank you! Unfortunately, I cannot echo the url of the image. The rwmb_meta returns an array, and I cannot echo the value of the array based on the key (url or full_url).

    #30745
    Long Nguyen
    Moderator

    Hi Janos,

    If the field is cloneable or has the setting 'multiple' => true, you need to use a loop to iterate through the array of images. Like this https://docs.metabox.io/fields/image-advanced/#template-usage

    #30752
    Janos
    Participant

    Thank you for the answer, helped a lot. I was choosing "Image" type in the Field Group. I changed that to "Single image", and I am able to pass through the meta key of the array now.

    Thanks!

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