您可以尝试以下代码段:
/**
* Shortcode for a series of elevating image files, excluding a range
*
* @link http://wordpress.stackexchange.com/a/151408/26350
*/
add_shortcode( \'episode\', \'episode_shortcode\' );
function episode_shortcode( $atts = array(), $content = \'\' )
{
//-----------------------
// Settings:
//
$path = \'/path/to/files\';
$ext = \'jpg\';
//-----------------------
// Shortcode input:
$atts = shortcode_atts(
array( \'num\' => 0, \'title\' => \'\', \'images\' => \'\' ),
$atts,
\'episode_shortcode\'
);
// Sanitize input:
$images = esc_attr( $atts[\'images\'] );
$title = esc_attr( $atts[\'title\'] );
$num = (int) $atts[\'num\'];
// Init:
$html = \'\';
$ranges = explode( \',\', $images );
// Loop over input ranges:
foreach( $ranges as $range )
{
$rng = explode( \'-\', $range );
if( count( $rng ) == 2 )
{
$from = (int) trim( $rng[0] );
$to = (int) trim( $rng[1] );
foreach( range( $from, $to ) as $i )
{
$html .= sprintf( \'<img src="%s/%d/%s.%s" alt="%s"/>\',
$path,
$num,
$i,
$ext,
$title
);
}
}
}
return $html;
}
一个非常大的循环跳过可能会更快
sprintf()
, 我在这里使用它是为了更好的可读性。