方法一:直接对a Img处理
原理,直接对输出的文章经行处理,正则表达式判断输出前直接对a img添加rel=”fancybox”,不过这样不符合W3c的标准。
使用方法,直接添加到fuctions.php中。
<code>
add_filter('the_content', 'addfancyboxclass_replace');
function addfancyboxclass_replace ($content)
{ global $post;
$pattern = "/<a(.*?)href=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>(.*?)<\/a>/i";
$replacement = '<a$1href=$2$3.$4$5 rel="fancybox" $6>$7</a>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
</code>
add_filter('the_content', 'addfancyboxclass_replace');
function addfancyboxclass_replace ($content)
{ global $post;
$pattern = "/<a(.*?)href=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>(.*?)<\/a>/i";
$replacement = '<a$1href=$2$3.$4$5 rel="fancybox" $6>$7</a>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
</code>
方法二:用jQuery 实现
原理,jQuery 遍历 网页,直接对a img元素处理。下面的代码是我主题上的…请参照官方说明改写~
<code>
jQuery(document).ready(function() {
var gallerybox = jQuery(".gallery a:has(img)").not(".nolightbox").filter(function() {
return /\.(jpe?g|png|gif|bmp)$/i.test(jQuery(this).attr('href'))
//遍历筛选
});
gallerybox.attr("rel", "yefengsxiangce");
var postimgbox = jQuery(".post a:has(img)").not(".nolightbox").filter(function() {
return /\.(jpe?g|png|gif|bmp)$/i.test(jQuery(this).attr('href'))
});
postimgbox.attr("rel", "yefengsimgbox");
//添加rel标签
$("a[rel=yefengsimgbox]").fancybox({
'overlayShow': true,
'transitionIn': 'elastic',
'transitionOut': 'elastic'
});
$("a[rel=yefengsxiangce]").fancybox({
'transitionIn': 'elastic',
'transitionOut': 'elastic',
'titlePosition': 'over',
'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
return '<span id="fancybox-title-over"> 相册 ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' ' + title: '') + '</span>'
}
})
});
</code>
jQuery(document).ready(function() {
var gallerybox = jQuery(".gallery a:has(img)").not(".nolightbox").filter(function() {
return /\.(jpe?g|png|gif|bmp)$/i.test(jQuery(this).attr('href'))
//遍历筛选
});
gallerybox.attr("rel", "yefengsxiangce");
var postimgbox = jQuery(".post a:has(img)").not(".nolightbox").filter(function() {
return /\.(jpe?g|png|gif|bmp)$/i.test(jQuery(this).attr('href'))
});
postimgbox.attr("rel", "yefengsimgbox");
//添加rel标签
$("a[rel=yefengsimgbox]").fancybox({
'overlayShow': true,
'transitionIn': 'elastic',
'transitionOut': 'elastic'
});
$("a[rel=yefengsxiangce]").fancybox({
'transitionIn': 'elastic',
'transitionOut': 'elastic',
'titlePosition': 'over',
'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
return '<span id="fancybox-title-over"> 相册 ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' ' + title: '') + '</span>'
}
})
});
</code>