繁体   English   中英

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

[英]Linking external JS and CSS files in WordPress

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

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

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

包含外部 CSS 和 JS 文件的正确方法是使用wp_enqueue_style method并在源参数上使用esc_url_raw

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

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

取决于您是否要添加 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.

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

            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

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

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

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

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

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

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

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

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

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

您无需购买任何插件,只需打开 WordPress 主题根目录下的 function.php 文件即可。 只需插入此函数PFB,只需更改目录,对于js,您不需要连接单独的文件,因为您可以使用footer.php并将您的js代码插入到脚本标签中,它会准确地工作。

add_action( 'wp_enqueue_scripts', 'radliv_child_enqueue_styles' );
    function radliv_child_enqueue_styles() {
    wp_enqueue_style( 'custom-css-style', get_template_directory_uri() . '/inc/assets/css/custom.css' );
} ;

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM