对所有图像使用相同标题的库:
这里有两种不同的方法可以动态实现这一点,而无需编辑库中每个图像的标题。
如果我们使用自定义属性same_caption
在我们的图库短代码中:
[gallery ids="1120,1123,1119" same_caption="Same Caption ..."]
然后我们可以得到该图库的相同标题。
Before:
After:这由以下插件(PHP 5.4+)独立支持:
插件#1-使用preg_replace()
:
<?php
/**
* Plugin Name: Gallery With Same Caption (#1)
* Description: Support the same_caption gallery attribute.
* Plugin URL: http://wordpress.stackexchange.com/a/187938/26350
* Author: Birgir Erlendsson (birgire)
* Version: 0.0.1
*/
add_action( \'init\', function()
{
add_shortcode( \'gallery\', function( $attr = [], $content = \'\' )
{
$output = gallery_shortcode( $attr );
if( isset( $attr[\'same_caption\'] ) )
{
$captiontag = isset( $attr[\'captiontag\'] ) ? tag_escape( $attr[\'captiontag\'] ) : \'dd\';
$from = "#<{$captiontag}( class=\'wp-caption-text gallery-caption\' )([^>]*)>([^<]*)</{$captiontag}>#";
$to = "<{$captiontag}\\\\1\\\\2>" . esc_html( $attr[\'same_caption\'] ) . "</{$captiontag}>";
$output = preg_replace( $from, $to, $output );
}
return $output;
} );
} );
插件2:使用
suppress_filters
还有一些其他挂钩:
<?php
/**
* Plugin Name: Gallery With Same Caption (#2)
* Description: Support the same_caption gallery attribute.
* Plugin URL: http://wordpress.stackexchange.com/a/187938/26350
* Author: Birgir Erlendsson (birgire)
* Version: 0.0.1
*/
namespace wpse\\birgire;
add_action( \'init\', function()
{
$o = new GalleryWithSameCaption;
$o->init();
} );
class GalleryWithSameCaption
{
private $caption;
public function init()
{
add_filter(
\'shortcode_atts_gallery\',
[ $this, \'shortcode_atts_gallery\' ],
10,
3
);
}
public function shortcode_atts_gallery( $out, $pair, $atts )
{
if( isset( $atts[\'same_caption\'] ) )
{
$this->caption = $atts[\'same_caption\'];
add_action( \'pre_get_posts\', [ $this, \'pre_get_posts\' ] );
}
return $out;
}
public function pre_get_posts( \\WP_Query $q )
{
remove_action( current_action(), __FUNCTION__ );
$q->set( \'suppress_filters\', false );
add_filter( \'the_posts\', [ $this, \'the_posts\' ] );
}
public function the_posts( $posts )
{
remove_filter( current_filter(), __FUNCTION__ );
return array_map( function( \\WP_Post $post )
{
$post->post_excerpt = esc_html( $this->caption );
return $post;
}, (array) $posts );
}
} // end class