DesmondNeo
DesmondNeo

Reputation: 11

How to link wp template to different stylesheet

I am trying have my own stylesheet linked to a custom page inside a theme on wordpress. Im using this code on my header.php

/my-own-styles.css" />

There are 2 changes in this code that I made: 'my-template.php' and 'my-own-styles.css' nothing other then that. (Do I need to change the 'template_directory' too?)

inside the theme directory I have 'my-own-styles.css' but it doesn't seem to get it.

also I need it to get a .js file that I have put in the same directory but wouldn't work..

Upvotes: 1

Views: 2413

Answers (2)

5wpthemes
5wpthemes

Reputation: 181

To link any page in wordpress whith a css file , just add this code in header.php

<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/yourfile.css" type="text/css" media="screen" />

Where css is a a folder in your theme directory. You also can use the code with path directly to your file.

Upvotes: 1

Andrew Bartel
Andrew Bartel

Reputation: 579

In WordPress, you need to hook your javascript and css includes onto the wp_enqueue_scripts action, and tell WordPress to load them using the wp_enqueue_style and wp_enqueue_script functions.

In your functions.php file, or any other file that will be loaded prior to the template file (say a plugin for example), add this:

add_action('wp_enqueue_scripts' , 'enqueue_my_scripts_and_styles');
function enqueue_my_scripts_and_styles() {
    wp_register_style('my-own-styles.css',home_url('/').'wp-content/themes/**yourthemename**/my-own-style.css');
    wp_enqueue_style('my-own-styles.css');

    wp_register_script('my-own-js.js',home_url('/').'wp-content/themes/**yourthemename**/my-own-js.js');
    wp_enqueue_script('my-own-js.js');

}

There are better ways to create the path to the file, but I wanted to provide an example that would be more obvious. For best practices, use get_template_directory_uri() http://codex.wordpress.org/Function_Reference/get_template_directory_uri

Upvotes: 1

Related Questions