如何将javascript添加到wordpress网站

How to add javascript to a wordpress site

本文关键字:wordpress 网站 添加 javascript      更新时间:2023-09-26

最近我从这个论坛上的另一个线程中找到了以下javascript;

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,但不幸的是丢失了页面)。

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

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

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

所以我的第一个想法,看 jsfiddle 是 js 版本,它被指定为没有包装。如果我更改其中任何一个,代码都不起作用。..所以1.我是否犯了一个新手错误,试图在我的函数中包含不兼容的js.php(可能是的?)和2。我可以进行简单的更改以使它在我的函数中工作.php?

我已经搜索了几个小时,并且确信我可以通过一些调整来解决这个问题?

仅供参考;功能.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
);
});
?>

在wordpress中,您可以使用wordpress api/hooks将javascript文件注入到主题中。 您想要的方法是 wp_enqueue_script .以下是文档

它是这样用的:

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

根据您拥有的 php 版本,您可以内联该函数:

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
    );
});

从 @atmd 提供的脚本中,以下代码有效。

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/文件夹中。发布的原始代码在网站上完美运行。