简体   繁体   English

我是否需要在Wordpress Child主题上创建或覆盖custom.js?

[英]Do I need to create or override custom.js on a Wordpress Child theme?

So, I've created a child theme from the University Wordpress.org theme. 因此,我从大学Wordpress.org主题创建了一个子主题。 Created a new style.css file (which references the parent theme) and have a functions.php that looks like this: 创建了一个新的style.css文件(该文件引用了父主题),并具有一个如下所示的functions.php:

<?php
 add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles',      PHP_INT_MAX);
function enqueue_child_theme_styles() {
wp_enqueue_style( 'university', get_template_directory_uri().'/style.css' );
wp_enqueue_style( 'university', get_template_directory_uri() . '/custom.css'      );
 }

So, that loads great. 所以,这很棒。 Except the whole reason for me creating a child theme was so that I could modify 'Camera.js' - which is included in the parent's js file. 除了我创建子主题的全部原因是为了修改“ Camera.js”(包含在父级js文件中)。

The camera.js file includes the following line.... camera.js文件包括以下行...。

fx : 'random', //'random','simpleFade', 'curtainTopLeft', 'curtainTopRight', 'curtainBottomLeft', 'curtainBottomRight', 'curtainSliceLeft', 'curtainSliceRight', 'blindCurtainTopLeft',

with a whole load of other effects you can choose to apply by putting a new effect name in place of the first random . 加上其他效果的全部负载,您可以选择应用新的效果名称代替第一个random

All I really want to do is have my child theme have just one of these effects applied without playing with the original Camera.js. 我真正想做的就是让我的孩子主题只应用其中一种效果,而不用玩原始的Camera.js。

The most obvious way I can think of doing this is duplicating the Camera.js file into my Child Theme folder and modifying it in there. 我想到的最明显的方法是将Camera.js文件复制到我的Child Theme文件夹中,然后在其中进行修改。 Would that work? 那行得通吗?

Thanks for any help! 谢谢你的帮助! Sorry if this has been asked elsewhere, I'm not entirely sure what I'm looking for to answer my question. 抱歉,如果在其他地方提出过此要求,我不确定要寻找什么来回答我的问题。

You cant over ride js files functionality so what you need to do is dequeue the original from your child theme and enqueue one from your child theme: 您无法超越ride js文件的功能,因此您需要做的是将原始主题从子主题中取出,并从子主题中取出一个:

eg dequeue script (you need to put in the handle the script was enqueued from) 例如出队脚本(您需要放入脚本入队的句柄)

function remove_script(){
    wp_dequeue_script('script_handle_used_to_enqueue');
    wp_enqueue_script( 'newhandle', get_stylesheet_directory_uri() . '/js/Camera.js');
}
add_action('wp_print_scripts', 'remove_script', 100);

Note the above assumes the js file was enqueued in the standard way. 请注意,以上假设js文件已以标准方式入队。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的 wordpress 主题 (Understrap) 中实现自定义 JS? - How do I implement custom JS into my wordpress theme (Understrap)? 如何在custom.js中收听Prestashop事件? - How can i listen to Prestashop events in custom.js? 如何在Wordpress子主题functions.php中正确排队retina.js? - How do I enqueue retina.js in a Wordpress child theme functions.php properly? 如果我不需要 SSR,如何编译 JS 文件以在 WordPress 主题中使用 React 组件? - How to compile the JS file to use a React component in a WordPress theme if I do not need SSR? IPython 3到Jupyter的迁移,custom.js和事件 - IPython 3 to Jupyter migration, custom.js and events 如何在for循环中调用custom.js文件,我想通过此js传递一些参数 - How to call custom.js file in for loop and i want pass some argument with this js 将输入中的数据发送到custom.js,并根据custom.js文件中的输入进行计算 - Send data from input to custom.js and make calculations on the basis of input in custom.js file 将自定义 Js 添加到 Wordpress 主题 - Adding Custom Js to Wordpress Theme 如何在wordpress子主题中添加自定义javascript - how to add custom javascript in wordpress child theme Drupal 7覆盖自定义主题中的jquery js文件 - Drupal 7 override jquery js file in custom theme
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM