【问题标题】:Colorbox for all Images in WordpressWordpress 中所有图像的颜色框
【发布时间】: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 之后再次打开了标签。

标签: wordpress colorbox


【解决方案1】:

自己找到了解决方案 - 默认上传链接在我的主题中以某种方式更改。我不得不将此行添加到functions.php:

update_option('image_default_link_type' , 'file'); 

那么,上一篇文章中的函数就可以正常工作了……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多