Create Shortcode to display in post or pages

Support General Create Shortcode to display in post or pagesResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #33309
    Martin RybosMartin Rybos
    Participant

    I'm completely lost in code as always so I'd rather ask before I spend hours with it.

    Can i create a shortcode to display fields as gallery or whatever?
    Images needs to use foreach to display it.

    foreach ( $digall as $key => $value ) {
    
    $image = RWMB_Image_Field::file_info( $value, ['size' => 'large'] );
     echo '<div class="swiper-slide"><img alt="" src="'. $image['url'] . '" /></div>';
    
    }

    But i need to create shortcode..

    function digallery($atts) {
        $a = shortcode_atts( array(
            'posts_per_page' => '',
            'gellery_id'  =>  ''
        ), $atts );
    
    $digalleries = rwmb_meta( 'digallery' );
    $digall = $digalleries['gallimage'];// Illegal string offset 'gallimage'
    
    $digallery = '';
    $post_in = esc_attr($a['testimonial_id']);
    $posts_per_page = esc_attr($a['posts_per_page']);
    
    $post_artay = explode(',', $post_in);
    $args = array(
        'post__in' => $post_artay,
        'posts_per_page' => $posts_per_page,
        'post_type'        => 'divart_gallery',
        'order_by' => 'post__in',
    );
    
    // the query
    $the_query = new WP_Query( $args );
    
    if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post();
    
    $digallery.= RWMB_Image_Field::file_info( $value, ['size' => 'large'] ); // to loop ?
    
    $digallery.= '<div class="title">'.get_the_title().'</div>';
    $digallery.= '<div class="content">'.get_the_content().'</div>';
    $digallery.='<div class="date">'.get_the_date().'</div>';
    $digallery.='<div class="author">'.get_the_author().'</div>';
    
    endwhile;
    endif;
    return $digallery;
    
    }
    add_shortcode('gallery', 'digallery' );
    
    //and shortcode would by
    
    [gallery posts_per_page="1" gellery_id="217"]

    Would anything like that be possible?
    Thank you

    #33331
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Please refer to this topic to know why the error message Illegal string offset 'gallimage' appears.
    https://support.metabox.io/topic/assign-sub-field-value-to-the-variable/

    If you want to show the gallery image subfield, you can put the helper function rwmb_meta() in the loop and pass the post ID to this function.

    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post();
    $digalleries = rwmb_meta( 'digallery', '', get_the_ID() );
    $image_ids = $digalleries['gallimage'];
    foreach ( $image_ids as $image_id ) {
        $image = RWMB_Image_Field::file_info( $image_id, ['size' => 'thumbnail'] );
        echo '<img src="' . $image['url'] . '">';
    }
    endwhile;
    endif;

    Refer to this documentation https://docs.metabox.io/extensions/meta-box-group/#sub-field-values

    #33341
    Martin RybosMartin Rybos
    Participant

    I love you !!
    You are great

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