javascript 在 WordPress 中链接外部 JS 和 CSS 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13449941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:48:53  来源:igfitidea点击:

Linking external JS and CSS files in WordPress

javascriptcsswordpress

提问by davidlee

I am new to Wordpress framework. May I know how I can link an external CSS and JS file to my Wordpress page? I have created a new page, but I would like to link a CSS and JS file to it.

我是 Wordpress 框架的新手。我可以知道如何将外部 CSS 和 JS 文件链接到我的 Wordpress 页面吗?我创建了一个新页面,但我想将 CSS 和 JS 文件链接到它。

Is creating a new template or plugin a solution? I am totally new to this.

创建新模板或插件是解决方案吗?我对此完全陌生。

采纳答案by Okpor Lawrence

Use the "Insert Header and Footer" wordpress plug-in. It does all the job for you! Just paste what should be in your into the header section of the plug-in.

使用“插入页眉和页脚”wordpress 插件。它为您完成所有工作!只需将您的内容粘贴到插件的标题部分即可。

回答by Pouya

I know this thread is a little dated but perhaps someone else might find it useful.

我知道这个帖子有点过时了,但也许其他人可能会觉得它很有用。

The proper way to include external CSS and JS files would be to use the wp_enqueue_style methodand using the esc_url_rawon the source parameter.

包含外部 CSS 和 JS 文件的正确方法wp_enqueue_style methodesc_url_raw在源参数上使用和。

For example to include t a google font into your theme you would od the following:

例如,要将 ta google 字体包含在您的主题中,您需要执行以下操作:

wp_enqueue_style( 'twentytwelve-googlefonts', esc_url_raw( 'http://fonts.googleapis.com/css?family=Titillium+Web:600,300,200' ), array(), null );

回答by barakadam

Depends if you want to add your CSS or JS conditionnaly. If you want them to be included in all files, just use the functions.php page of your theme folder, and add :

取决于您是否想添加 CSS 或 JS 条件。如果您希望它们包含在所有文件中,只需使用主题文件夹的 functions.php 页面,并添加:

            function your_scripts() {
                    wp_register_script('yourscriptname', '/PATH/TO/YOUR/SCRIPT.js', false);
                    // This registers your script with a name so you can call it to enqueue it
                    wp_enqueue_script( 'yourscriptname' );
                    // enqueuing your script ensures it will be inserted in the propoer place in the header section
            }
            add_action('wp_enqueue_scripts', 'your_scripts');
            // This hook executes the enqueuing of your script at the proper moment.

For the stylesheet, proceed this way :

对于样式表,请按以下方式进行:

            function your_css() {
                wp_register_style( 'nameofyourCSSsheet', '/PATH/TO/YOUR/STYLESHEET.css' );
                wp_enqueue_style( 'nameofyourCSSsheet' );
            }
            add_action( 'wp_enqueue_scripts', 'your_css' );
            // same hook is used to enqueue CSS sheets

回答by Matti Mehtonen

You can add files to your current theme. Add css-files to header (header.php in your theme folder). And add js-files to footer (footer.php in your theme folder). Theme folder can be found in path wp-content/themes/

您可以将文件添加到当前主题。将 css-files 添加到标题(主题文件夹中的 header.php)。并将js文件添加到页脚(主题文件夹中的footer.php)。主题文件夹可以在路径 wp-content/themes/ 中找到

回答by Kaleem Sajid

You can modify header.php file of your theme to include external JSand CSSfiles. Header.php is located in wp-content>themes>currently active theme>header.php.

您可以修改主题的 header.php 文件以包含外部JSCSS文件。Header.php 位于 wp-content>themes>currently active theme>header.php。

Open it in an editor and include a link to external or files between <head></head>tag.

在编辑器中打开它并在<head></head>标签之间包含指向外部或文件的链接。

回答by user1833715

You don't need to create a plugin or a new template to link to a file within an existing Wordpress theme.

您不需要创建插件或新模板来链接到现有 Wordpress 主题中的文件。

As suggested if you open up your themes header.php and before the closing tag you can insert a link to the file location.

根据建议,如果您打开主题 header.php,并且在结束标记之前,您可以插入指向文件位置的链接。

Your CSS file will generally reside within your theme folder: wp-content/themes/your_theme/style.css

您的 CSS 文件通常位于您的主题文件夹中: wp-content/themes/your_theme/style.css

You can call the stylesheet using the following Wordpress function:

您可以使用以下 Wordpress 函数调用样式表:

Take a look at the following Wordpress Codex section for info: http://codex.wordpress.org/Function_Reference/bloginfo

查看以下 Wordpress Codex 部分的信息:http: //codex.wordpress.org/Function_Reference/bloginfo