Support

Account

Home Forums General Issues Modify function to take start argument and no of pictures Reply To: Modify function to take start argument and no of pictures

  • add_shortcode’s callback accepts 2 parameters. The first one being the attributes and the second on is the body content. https://developer.wordpress.org/reference/functions/add_shortcode/

    Then you can use php’s array_slice to to slice the array with a starting index and length. http://php.net/manual/en/function.array-slice.php

    So, you callback will look something like this:

    
    function allphotos_shortcode($attrs) {
        $attrs = shortcode_atts([
            'start' => 0,
            'number' => null,
        ], $attrs);
    
        if (! $images = get_field('fl_gallery')) {
            return '';
        }
    
        if (! $images = array_slice($images , $attrs['start'], $attrs['number'])) {
            return '';
        }
    
        $output .= '<ul id="kalpht">';
        foreach ($images as $image) {
            $output .= sprintf(
                '<li><a class="x-img-link" href="%s" data-options="%s">%s</a></li>',
                $image['url'],
                "thumbnail:'{$image['sizes']['mini-me']}'",
                wp_get_attachment_image($image['id'], 'mini-me')
            );
        }
        $output .= '</ul>' . do_shortcode('[lightbox opacity="0.9" thumbnails="true"]');
    
        return $output;
    }

    cheers