如何将JavaScript添加到Wordpress网站 [英] How to add javascript to a wordpress site

查看:54
本文介绍了如何将JavaScript添加到Wordpress网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我在该论坛的另一个线程中找到了以下javascript;

Recently I found the following javascript from another thread on this forum;

var $content=$('div.leg_ol');
var $links=$('.leg_test').hover(function(){
var index= $links.index(this);
$content.stop(true,true).hide().eq(index).fadeIn();
},function(){
$content.stop(true,true).hide().eq(index);
});

(我会链接到OP,但不幸的是丢失了该页面).

(I would link to the OP, but unfortunately have lost the page).

JSFIDDLE: https://jsfiddle.net/mfyctwvs/1/

JSFIDDLE: https://jsfiddle.net/mfyctwvs/1/

代码完全可以完成我想做的事情-从理论上讲,现在我对js几乎是一个全新的领域,所以这对我来说是一个非常棘手的领域-请耐心等待.

The code does exactly what I want to do - in theory, now I am pretty much completely new to js, so this is a very tricky area for me - please bear with me on this.

当我将代码发布到functions.php中时,它会导致整个站点停止工作,我认为是因为它无法读取它或存在一些冲突?

When I post the code in functions.php it causes my whole site to stop working, I assume because it cannot read it or there is some conflict?

所以我首先想到的是jsfiddle,它是js版本,并且被指定为no wrap in.如果我更改了这两个代码,代码将无法正常工作..so 1.我在尝试将不兼容的js包含在我的functions.php中时犯了一个newb错误(可能是吗?)&2.是否可以进行简单的更改以使其在我的functions.php中正常工作?

So my first thought, looking at jsfiddle was the js version and that it is specified as no wrap in . If I change either of these the code does not work.. ..so 1. Am I making a newb mistake trying to include incompatible js in my functions.php (probably yes?) & 2. is there a straightforward change I can make to get this working in my functions.php?

我一直在寻找这个小时和一个小时.确定可以通过一些调整使它正常工作吗?

I have been searching on this for hours & am sure that I could get this working with some adjustments?

仅供参考;Functions.php

FYI; Functions.php

  <?php// Set path to WooFramework and theme specific functions 
$functions_path = get_template_directory() . '/functions/';
$includes_path = get_template_directory() . '/includes/';

// Don't load alt stylesheet from WooFramework
if ( ! function_exists( 'woo_output_alt_stylesheet' ) ) {
function woo_output_alt_stylesheet () {}
}

// WooFramework
require_once ( $functions_path . 'admin-init.php' );    // Framework Init

if ( get_option( 'woo_woo_tumblog_switch' ) == 'true' ) {
//Enable Tumblog Functionality and theme is upgraded
update_option( 'woo_needs_tumblog_upgrade', 'false' );
update_option( 'tumblog_woo_tumblog_upgraded', 'true' );
update_option( 'tumblog_woo_tumblog_upgraded_posts_done', 'true' );
require_once ( $functions_path . 'admin-tumblog-quickpress.php' );  //   Tumblog Dashboard Functionality
}

/*-----------------------------------------------------------------------------------*/


$includes = array(
            'includes/theme-options.php',               // Options panel settings and custom settings
            'includes/theme-functions.php',             // Custom theme functions
            'includes/theme-actions.php',               // Theme actions & user defined hooks
            'includes/theme-comments.php',              // Custom comments/pingback loop
            'includes/theme-js.php',                    // Load JavaScript via wp_enqueue_script
            'includes/theme-plugin-integrations.php',   // Plugin integrations
            'includes/sidebar-init.php',                // Initialize widgetized areas
            'includes/theme-widgets.php',               // Theme widgets
            'includes/theme-advanced.php',              // Advanced Theme Functions
            'includes/theme-shortcodes.php',            // Custom theme shortcodes
            'includes/woo-layout/woo-layout.php',       // Layout Manager
            'includes/woo-meta/woo-meta.php',           // Meta Manager
            'includes/woo-hooks/woo-hooks.php'          // Hook Manager
            );

// Allow child themes/plugins to add widgets to be loaded.
$includes = apply_filters( 'woo_includes', $includes );

foreach ( $includes as $i ) {
locate_template( $i, true );
}

// Load WooCommerce functions, if applicable.
if ( is_woocommerce_activated() ) {
locate_template( 'includes/theme-woocommerce.php', true );
}

    /*-----------------------------------------------------------------------------------*/
/* You can add custom functions below */
/*-----------------------------------------------------------------------------------*/


 add_action( 'init', 'woo_custom_move_navigation', 10 );

function woo_custom_move_navigation () {
// Remove main nav from the woo_header_after hook
remove_action( 'woo_header_after','woo_nav', 10 );
// Add main nav to the woo_header_inside hook
add_action( 'woo_header_inside','woo_nav', 10 );
} // End woo_custom_move_navigation()

/* Testing stuff for mobile */
function woo_load_responsive_meta_tags () {
    $html = '';

    $html .= "\n" . '<!-- Always force latest IE rendering engine (even      in intranet) & Chrome Frame -->' . "\n";
    $html .= '<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />' . "\n";

    echo $html;
} // End woo_load_responsive_meta_tags()


add_action('wp_enqueue_scripts', function () {
wp_enqueue_script(
   'user-scripts', 
   get_template_directory_uri() . '/functions/user-scripts.js', 
   array('jquery') // any script dependancies. i.e. jquery
);
});
?>

推荐答案

在@atmd提供的脚本中,以下代码有效.

From the script provided by @atmd the following code worked.

add_action('wp_enqueue_scripts', function () {
wp_enqueue_script(
   'script-name', 
   get_template_directory_uri() . '/path-to-your-script.js', 
   array('jquery') // any script dependancies. i.e. jquery
);
});

所需的先决条件是脚本位于所使用主题的/functions/文件夹中.发布的原始代码在该网站上可以完美运行.

A precondition required was that the script was located in the /functions/ folder of the theme used. The original code posted works perfectly on the site.

这篇关于如何将JavaScript添加到Wordpress网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆