How to display this in Views

Support MB Views How to display this in ViewsResolved

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #34478
    smt ontwerpstudiosmt ontwerpstudio
    Participant

    Hi,
    I am trying to display the category image in a View. I found this PHP in a tutorial, but how to turn this code into Twig language?

    
    <?php
        $terms= get_the_terms( $post->ID, 'portfolio-type');
        $background_image = get_term_meta( $terms[0]->term_id, 'upload_portfolio_thumbnail', true );
        if ($background_image) {
            $link_image = wp_get_attachment_image_src( $background_image, 'full' );
            $link_image_source = $link_image[0];
        }
        else {
            $link_image_source = get_term_meta( $terms[0]->term_id, 'url_portfolio_thumbnail', true );
        }
        if ( !empty( $terms ) ){
            $term = array_shift( $terms );
        }
    ?>
    <div class="port-thumbnail">
        <img class="thumbnail-cat" src="<?php echo $link_image_source ?>">
    </div>
    <div class="portfolio-heading"><?php echo $term->name; ?></div>
    <div class="portfolio-description"><?php echo category_description(the_category_id()); ?> </div>
    
    #34491
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can follow this documentation to know how to use Twig
    https://docs.metabox.io/extensions/mb-views/#twig

    and combine it with the proxy mb to call a PHP function in View
    https://docs.metabox.io/extensions/mb-views/#running-php-functions

    For example:

    $terms = get_the_terms( $post->ID, 'portfolio-type');
    $background_image = get_term_meta( $terms[0]->term_id, 'upload_portfolio_thumbnail', true );
    {% set terms = mb.get_the_terms( post.ID, 'portfolio-type' ) %}
    {% set background_image = mb.get_term_meta( terms[0].term_id, 'upload_portfolio_thumbnail', true ) %}

    and so on.

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