动态观察滑块和事件管理器插件JavaScript

时间:2012-11-22 作者:Danny Michel

我有这个滑块(环绕滑块http://foundation.zurb.com/docs/orbit.php ) 和wordpress插件(事件管理器http://wp-events-plugin.com/ ) 加载所有jQuery ui内容(

/**
   * Enqueing public scripts and styles
   */
  function public_enqueue() {
    //Scripts
    wp_enqueue_script(\'events-manager\', plugins_url(\'includes/js/events-manager.js\',__FILE__), array(\'jquery\', \'jquery-ui-core\',\'jquery-ui-widget\',\'jquery-ui-position\',\'jquery-ui-sortable\',\'jquery-ui-datepicker\',\'jquery-ui-autocomplete\',\'jquery-ui-dialog\')); //jQuery will load as dependency
    //Styles
    wp_enqueue_style(\'events-manager\', plugins_url(\'includes/css/events_manager.css\',__FILE__)); //main css
  }

  /**
   * Localize the script vars that require PHP intervention, removing the need for inline JS.
   */
  function localize_script(){
    global $em_localized_js;
    $locale_code = substr ( get_locale(), 0, 2 );
    //Localize
    $em_localized_js = array(
      \'ajaxurl\' => admin_url(\'admin-ajax.php\'),
      \'locationajaxurl\' => admin_url(\'admin-ajax.php?action=locations_search\'),
      \'firstDay\' => get_option(\'start_of_week\'),
      \'locale\' => $locale_code,
      \'dateFormat\' => get_option(\'dbem_date_format_js\'),
      \'ui_css\' => plugins_url(\'includes/css/ui-lightness.css\', __FILE__),
      \'show24hours\' => get_option(\'dbem_time_24h\'),
      \'is_ssl\' => is_ssl(),
    );
)在网站的每个页面的顶部(

<link rel=\'stylesheet\' id=\'events-manager-css\'  href=\'http://at.theplural.com/wp-content/plugins/events-manager/includes/css/events_manager.css?ver=3.4.2\' type=\'text/css\' media=\'all\' />
<script type=\'text/javascript\' src=\'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js\'></script>
<script type=\'text/javascript\' src=\'http://at.theplural.com/wp-includes/js/jquery/ui/jquery.ui.core.min.js?ver=1.8.20\'></script>
<script type=\'text/javascript\' src=\'http://at.theplural.com/wp-includes/js/jquery/ui/jquery.ui.widget.min.js?ver=1.8.20\'></script>
<script type=\'text/javascript\' src=\'http://at.theplural.com/wp-includes/js/jquery/ui/jquery.ui.position.min.js?ver=1.8.20\'></script>
<script type=\'text/javascript\' src=\'http://at.theplural.com/wp-includes/js/jquery/ui/jquery.ui.mouse.min.js?ver=1.8.20\'></script>
<script type=\'text/javascript\' src=\'http://at.theplural.com/wp-includes/js/jquery/ui/jquery.ui.sortable.min.js?ver=1.8.20\'></script>
<script type=\'text/javascript\' src=\'http://at.theplural.com/wp-includes/js/jquery/ui/jquery.ui.datepicker.min.js?ver=1.8.20\'></script>
<script type=\'text/javascript\' src=\'http://at.theplural.com/wp-includes/js/jquery/ui/jquery.ui.autocomplete.min.js?ver=1.8.20\'></script>
<script type=\'text/javascript\' src=\'http://at.theplural.com/wp-includes/js/jquery/ui/jquery.ui.resizable.min.js?ver=1.8.20\'></script>
<script type=\'text/javascript\' src=\'http://at.theplural.com/wp-includes/js/jquery/ui/jquery.ui.draggable.min.js?ver=1.8.20\'></script>
<script type=\'text/javascript\' src=\'http://at.theplural.com/wp-includes/js/jquery/ui/jquery.ui.button.min.js?ver=1.8.20\'></script>
<script type=\'text/javascript\' src=\'http://at.theplural.com/wp-includes/js/jquery/ui/jquery.ui.dialog.min.js?ver=1.8.20\'></script>
)。我基本上是试图阻止javascript加载到首页,或者理想情况下,只加载到->页面(“事件”)上所需的特定页面。我尽了最大努力让js“出列”,但没用

1 个回复
最合适的回答,由SO网友:jzatt 整理而成

如果只想在“事件”页面上对脚本排队,请更改以下代码以包含条件标记is_page()

function public_enqueue() {
    //Scripts
    wp_enqueue_script(\'events-manager\', plugins_url(\'includes/js/events-manager.js\',__FILE__), array(\'jquery\', \'jquery-ui-core\',\'jquery-ui-widget\',\'jquery-ui-position\',\'jquery-ui-sortable\',\'jquery-ui-datepicker\',\'jquery-ui-autocomplete\',\'jquery-ui-dialog\')); //jQuery will load as dependency
    //Styles
    wp_enqueue_style(\'events-manager\', plugins_url(\'includes/css/events_manager.css\',__FILE__)); //main css
}
将其更改为以下内容:

function public_enqueue() {
    if( is_page(\'events\') ) { //Only load on the page "events"
        //Scripts
        wp_enqueue_script(\'events-manager\', plugins_url(\'includes/js/events-manager.js\',__FILE__), array(\'jquery\', \'jquery-ui-core\',\'jquery-ui-widget\',\'jquery-ui-position\',\'jquery-ui-sortable\',\'jquery-ui-datepicker\',\'jquery-ui-autocomplete\',\'jquery-ui-dialog\')); //jQuery will load as dependency
        //Styles
        wp_enqueue_style(\'events-manager\', plugins_url(\'includes/css/events_manager.css\',__FILE__)); //main css
    }
}

结束

相关推荐

require.js to load javascript

虽然wp\\u enqueue\\u脚本似乎可以工作,但它并不是很优雅。我目前正在制作一个使用MVC(backbone.js)的WP前端,它有30多个独立的模型/视图/集合/控制器脚本,我一直在增加这个数量。我用过require。js以前很喜欢它,但它似乎完全打破了WordPress的逻辑。有谁能告诉我一个在wordpress中使用js脚本加载器的干净方法吗?还是这是一项没有希望的任务?谢谢