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
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.