If you want limit the number of items in a gallery, you can use:
[gallery maxitems="50"]
wherethere seems to be a plugin available on wordpress.org called Limit parameter for gallery shortcode (no affiliation). It uses the custom itemspre_get_posts
attribute isfilter to change the maximum numberposts_per_page
and offset
attributes of items to displaythe get_posts()
or get_children()
calls in the gallery shortcode callback.
This attribute is supported by the following pluginExample:
<?php
/**
* Plugin Name: The Gallery Shortcode - Maximum Number Of Items.
* Description: Support the 'maxitems' attribute in the gallery shortcode
* Plugin URI: http://wordpress.stackexchange.com/a/176937/26350
* Plugin Author: Birgir Erlendsson (birgire)
* Version: 0.0.2
*/
add_action( 'wp', function()
{
if( class_exists( 'GalleryMaxItems' ) )
{
$o = new GalleryMaxItems;
$o->init();
}
} );
class GalleryMaxItems
{
private $maxitems;
public function init()
{
add_filter( 'post_gallery', array( $this, 'post_gallery' ), 10, 3 );
}
public function post_gallery( $output = '', $attr = array(), $instance = 0 )
{
if( isset( $attr['maxitems'] ) )
{
$this->maxitems = $attr['maxitems'];
add_filter( 'query', array( $this, 'query' ) );
}
return $output;
}
public function query( $sql )
{
remove_filter( current_filter(), array( $this, __FUNCTION__ ) );
$limit = sprintf( " LIMIT 0, %d ", $this->maxitems );
$regex = '#LIMIT 0,\s\d+#';
if( false !== stripos( $sql, ' LIMIT ' ) )
$sql = preg_replace( $regex, $limit, $sql );
else
$sql .= $limit;
return $sql;
}
}[gallery //id="4" endlimit="3" classoffset="1"]