【发布时间】:2012-11-20 17:26:28
【问题描述】:
我想在我的 Wordpress 博客中的所有图片中使用 Colorbox。 我正在使用此命令在我的 functions.php 中添加 rel='colorbox':
//colorbox
// adds the colorbox jQuery code
function insert_colorbox_js() {
?>
<script type="text/javascript">
// <![CDATA[
jQuery(document).ready(function($){
$("a[rel='colorbox']").colorbox({
transition:'elastic',
opacity:'0.7',
maxHeight:'90%'
});
$("a[rel='colorboxvideo']").colorbox({
iframe:true,
transition:'elastic',
opacity:'0.7',
innerWidth:'60%',
innerHeight:'80%'
});
});
// ]]>
</script>
<?php
}
add_action( 'wp_head', 'insert_colorbox_js' );
// automatically add colorbox rel attributes to embedded images
function insert_colorbox_rel($content) {
$pattern = '/<a(.*?)href="(.*?).(bmp|gif|jpeg|jpg|png)"(.*?)>/i';
$replacement = '<a$1href="$2.$3" rel=\'colorbox\'$4>';
$content = preg_replace( $pattern, $replacement, $content );
return $content;
}
add_filter( 'the_content', 'insert_colorbox_rel' );
此函数条目在我的本地 Mamp 设置中运行良好,但由于我将网站移动到我的服务器,它不再工作 - rel 属性被添加到 img 标签而不是链接:
<div id="attachment_1131" class="wp-caption alignleft" style="width: 160px"><a href="http://www.somesite.net/somelink/" rel="attachment wp-att-1131"><img class="size-thumbnail wp-image-1131" title="Some title" src="http://www.somesite.net/wp-content/uploads/2011/04/CIMG0935-150x150.jpg" rel='colorbox' alt="Some Text" width="150" height="150" /></a><p class="wp-caption-text">Some other Text</p></div>
我必须如何更改 functions.php 以使其再次工作?
感谢您的帮助!
顺便说一句 - 我使用的是最新的 Wordpress 3.4.2
【问题讨论】:
-
我因为 Javascript 而关闭了 PHP 标签 - 我在 Javascript 之后再次打开了标签。