在库短代码处理程序和输出之间设置自定义函数。抓住img
元素并添加静态计数器。然后将gallery输出返回到WordPress。
示例代码:
add_action( \'after_setup_theme\', \'wpse_74492_replace_gallery_shortcode\' );
/**
* Replace the default shortcode handlers.
*
* @return void
*/
function wpse_74492_replace_gallery_shortcode()
{
remove_shortcode( \'gallery\' );
add_shortcode( \'gallery\', \'wpse_74492_gallery_shortcode\' );
}
function wpse_74492_gallery_shortcode( $attr )
{
// Let WordPress create the regular gallery …
$gallery = gallery_shortcode( $attr );
$gallery = preg_replace_callback( \'~<img~\', \'wpse_74492_gallery_callback\', $gallery );
return $gallery;
}
function wpse_74492_gallery_callback( $matches )
{
static $count = 0;
$count += 1;
return "<span class=\'gallery-number\'>$count</span>" . $matches[0];
}
这将插入
<span class=\'gallery-number\'>1</span>
进入链接。您可以通过以下方式在CSS中定位它:
.gallery-number
{
position: absolute;
left:0;
top: 1em;
}