如何将javascript添加到wordpress站点

时间:2015-03-17 08:59:57

标签: javascript php wordpress

最近我在这个论坛的另一个帖子中发现了以下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来说几乎是全新的,所以对我来说这是一个非常棘手的领域 - 请耐心等待。

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

所以我的第一个想法是,查看jsfiddle是js版本,并且它被指定为无包装。如果我更改其中任何一个代码都不起作用.. .. 1.我是否犯了一个新错误,试图在我的functions.php中包含不兼容的js(可能是的?)& 2.我可以通过我的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
);
});
?>

2 个答案:

答案 0 :(得分:1)

在wordpress中,您可以使用wordpress api / hooks将javascript文件转换为主题。你想要的方法是wp_enqueue_scriptHere are the docs

它的使用方式如下:

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

答案 1 :(得分:-2)

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