【问题标题】:Wordpress Plugin and JqueryWordpress 插件和 Jquery
【发布时间】:2010-09-10 22:02:54
【问题描述】:

我正在尝试通过我正在制作的插件创建画廊,但遇到了 javascript 错误并且无法弄清楚原因。以下是我的代码,任何帮助将不胜感激。这是我实现它的地方。 http://findlegalanswers.com/?page_id=4

<?php
/*
Plugin Name: iPad and iPhone Swipe Gallery
Version: 1.0
Plugin URI: http://www.prototypesyndicate.com
Description: Adds a great simple looking gallery to your pages or post with iPhone and iPad swipe. For more information on how to setup view documentation in plugin folder
Author: Alex Gonzalez - Vinas
Author URI: http://www.prototypesyndicate.com
*/


    function jquery_load_gallery_header() {
        echo "<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js\" type=\"text/javascript\"></script>" . "\n";
        echo "<script src=" . get_bloginfo('wpurl') . "/wp-content/plugins/iPad_iPhone_Gallery/js/jquery.touch-gallery-1.0.0.js\"></script>" . "\n";
    }

    function simpleslide_load_ready()
    {
        echo "<style type=\"text/css\">

        #content {
            padding: 50px 0 0 0;
            width: 600px;
        }
        #gallery {
            width: 300px;
            overflow: hidden;
        }
        #gallery div,
        #gallery a {
            display: block;
            float: left;
            color: #fff;
            width: 75px;
            height: 75px;
            margin: 0 8px 8px 0;
        }

            </style>";
    }

add_action('wp_head', 'jquery_load_gallery_header');
add_action('wp_head', 'simpleslide_load_ready');


//album [album category="flickr_url"] [/album]
function mAlbum( $atts, $content = null ) {

   extract( shortcode_atts( array(
      'flickr' => 'Featured',
      ), $atts ) );

    ob_start();
    ?>

    <div id="content">
        <div id="gallery">
        </div>
    </div>

    <script>
        $(function() {
            $.getJSON("http://api.flickr.com/services/rest?method=flickr.photosets.getPhotos&api_key=ed144a125aca366df3438c58c0c0ec9d&photoset_id=72157624601158052&extras=url_sq,url_m,url_o,&format=json&jsoncallback=?", function(data) {
                $.each(data.photoset.photo, function(i) {
                    $('<div>').append($('<img>').attr('src', this.url_sq)).data('flickr', this).appendTo('#gallery');
                });
                $('#gallery div').touchGallery({
                    getSource: function() {
                        var f = $(this).data('flickr');
                        return f.url_o || f.url_sq.replace('_s.', '_b.');
                    }
                });
            });

        });
    </script>


<?php   

    $content = ob_get_contents();
    ob_end_clean();
    return $content;

}

// Adds shortcode to use in pages or post. 
add_shortcode('album', 'mAlbum');


?>

【问题讨论】:

  • 你的功能touchGallery在哪里?
  • 您的第二个脚本标签似乎缺少开头引号和脚本类型。
  • 使用plugins_url函数(phpxref.ftwr.co.uk/wordpress/nav.html?wp-includes/…)代替完全硬编码你的js文件的url。类似plugins_url( 'js/jquery.touch-gallery-1.0.0.js', __FILE__ );
  • 谢谢,问题出在我想弄清楚的时候。

标签: jquery wordpress


【解决方案1】:

我将归咎于污染的“wpurl”回报。猜测在您的博客标题设置的末尾有一个空格,它被 urlencode 到了一个奇怪的目的地。因此,网络浏览器寻找:

http://blabla%20/wp-content/...

无论如何,浏览器都会以这种方式被抛弃。根据需要检查您的博客标题、修剪空格或以其他方式对整个 URL 进行硬编码(无论好坏)。

罪魁祸首:

echo "<script src=" . get_bloginfo('wpurl') . "/wp-content/plugins/iPad_iPhone_Gallery/js/jquery.touch-gallery-1.0.0.js\"></script>" . "\n";

【讨论】:

  • wpurl 返回站点的 URL 而不是标题。如果它被污染了,该网站的大部分其他部分也将无法工作。此外,硬编码 URL 是一个坏主意(在公开发布的插件的情况下是不可行的,我猜这可能是)。
  • 冒着听起来卑鄙的风险,您将“应该”与“过去”混为一谈。昨天我对那个网站进行了firebug 时,URL 中有一个奇怪的字符编码。 =)
猜你喜欢
  • 1970-01-01
  • 2015-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-01
  • 1970-01-01
  • 2014-05-29
  • 2012-11-14
相关资源
最近更新 更多