媒体上载程序将图像和标题包装在[caption]
短代码。有一个名为img_caption_shortcode
那可以做你想做的事。标题本身作为数组的一部分传递给短代码处理程序,图像id也是这样。
add_filter( \'img_caption_shortcode\', \'wwm_img_caption_filter\', 10, 3 );
function wwm_img_caption_filter( $empty, $attr, $content )
{
$image_id = $attr[\'id\'];
//do some checking of the $image_id (attachment id) to see what it links to
//if it links to amazon, include the link in the caption:
if ( /*my condition is met*/ ) {
$attr[\'caption\'] = ;//modify the string appropriately
}
return ;//some string - see documentation
}
以下是有关过滤器的一些文档:
http://codex.wordpress.org/Plugin_API/Filter_Reference/img_caption_shortcode 使现代化**
如果所讨论的图像没有标题,请使用\\u内容过滤器
add_filter( \'the_content\', \'add_affiliate_link_caption\' );
function add_affiliate_link_caption( $content )
{
//I\'m not even going to try a regex...
//but some sort of regex find/replace stuff on $content
return $content;
}