这是一个创建于 4764 天前的主题,其中的信息可能已经有所发展或是发生改变。
我想让WP插入图片时自动生成链接的rel值,并使用img的caption作为链接抬头。
即 <a href="my img" rel="prettyPhot[postID]" title="my img capiton"><img...></a>
谷歌到款插件如下:
add_filter('the_content', 'addlightboxtitle_replace', 99); // Filter for Post
add_filter('the_excerpt', 'addlightboxtitle_replace', 99); // Filter for Excerpt
add_filter('get_comment_text', 'addlightboxtitle_replace', 99); // Filter for Comment
function addlightboxtitle_replace ($content) {
global $post;
// [0] <a xyz href="...(.bmp|.gif|.jpg|.jpeg|.png)" zyx>yx</a> --> <a href="...(.bmp|.gif|.jpg|.jpeg|.png)" xyz zyx>yx</a>
$pattern[0] = "/(<a)([^\>]*?) href=('|\")([^\>]*?)(\.bmp|\.gif|\.jpg|\.jpeg|\.png)('|\")([^\>]*?)>(.*?)<\/a>/i";
$replacement[0] = '$1 href=$3$4$5$6$2$7>$8</a>';
// [1] <a href="...(.bmp|.gif|.jpg|.jpeg|.png)" xyz zyx>yx</a> --> <a href="...(.bmp|.gif|.jpg|.jpeg|.png)" rel="lightbox[POST-ID]" xyz zyx>yx</a>
$pattern[1] = "/(<a href=)('|\")([^\>]*?)(\.bmp|\.gif|\.jpg|\.jpeg|\.png)('|\")([^\>]*?)(>)(.*?)(<\/a>)/i";
$replacement[1] = '$1$2$3$4$5 rel="lightbox['.$post->ID.']"$6$7$8$9';
// [2] <a href="...(.bmp|.gif|.jpg|.jpeg|.png)" rel="lightbox[POST-ID]" xyz rel="(lightbox|nolightbox)yxz" zyx>yx</a> --> <a href="...(.bmp|.gif|.jpg|.jpeg|.png)" xyz rel="(lightbox|nolightbox)yxz" zyx>yx</a>
$pattern[2] = "/(<a href=)('|\")([^\>]*?)(\.bmp|\.gif|\.jpg|\.jpeg|\.png)('|\") rel=('|\")lightbox([^\>]*?)('|\")([^\>]*?) rel=('|\")(lightbox|nolightbox)([^\>]*?)('|\")([^\>]*?)(>)(.*?)(<\/a>)/i";
$replacement[2] = '$1$2$3$4$5$9 rel=$10$11$12$13$14$15$16$17';
// [3] <a href="...(.bmp|.gif|.jpg|.jpeg|.png)" xyz>yx title=yxz xy</a> --> <a href="...(.bmp|.gif|.jpg|.jpeg|.png)" xyz title=yxz>yx title=yxz xy</a>
$pattern[3] = "/(<a href=)('|\")([^\>]*?)(\.bmp|\.gif|\.jpg|\.jpeg|\.png)('|\")([^\>]*?)(>)(.*?) title=('|\")(.*?)('|\")(.*?)(<\/a>)/i";
$replacement[3] = '$1$2$3$4$5$6 title=$9$10$11$7$8 title=$9$10$11$12$13';
// [4] <a href="...(.bmp|.gif|.jpg|.jpeg|.png)" xyz title=zxy xzy title=yxz>yx</a> --> <a href="...(.bmp|.gif|.jpg|.jpeg|.png)" xyz title=zxy xzy>yx</a>
$pattern[4] = "/(<a href=)('|\")([^\>]*?)(\.bmp|\.gif|\.jpg|\.jpeg|\.png)('|\")([^\>]*?) title=([^\>]*?) title=([^\>]*?)(>)(.*?)(<\/a>)/i";
$replacement[4] = '$1$2$3$4$5$6 title=$7$9$10$11';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
?>
我把相应参数改成prettyPhoto后rel值没有问题了,title也会取img抬头。不过我想用caption来定义title。。。请问该如何改动呢?