最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Linking external JS and CSS files in WordPress - Stack Overflow

programmeradmin3浏览0评论

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.

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

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.

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

Share Improve this question edited Feb 16, 2017 at 22:03 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked Nov 19, 2012 at 8:19 davidleedavidlee 6,16718 gold badges58 silver badges87 bronze badges 1
  • You can just load the files in header.php as you'd normally do with a static html page. – elclanrs Commented Nov 19, 2012 at 8:20
Add a comment  | 

7 Answers 7

Reset to default 9

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 method and using the esc_url_raw on the source parameter.

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

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

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 :

            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

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.

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/

You can modify header.php file of your theme to include external JS and CSS files. Header.php is located in 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.

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

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

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

You can call the stylesheet using the following Wordpress function:

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

You don't need to purchase any plugin, just open function.php file inside your root directory of WordPress theme. and just insert this function PFB, just change the directory, for js you don't need to connect a separate file because you can use footer.php and insert your js code in script tag it will work accurately.

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' );
} ;
发布评论

评论列表(0)

  1. 暂无评论