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

javascript - How to Link a Script In WordPress - Stack Overflow

programmeradmin8浏览0评论

My script doesn't work in Wordpress. I'm trying to convert my HTML page into a Wordpress Theme.. Ive got the CSS linked..

<link type="text/css" rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/min/mycss.css" />     

but my script doesn't connect..

<script type="text/javascript" src="min/myjs.js"></script>

i tried the same , using the <?php bloginfo('template_directory'); ?>

<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/min/myjs.js"></script>

Help?

My script doesn't work in Wordpress. I'm trying to convert my HTML page into a Wordpress Theme.. Ive got the CSS linked..

<link type="text/css" rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/min/mycss.css" />     

but my script doesn't connect..

<script type="text/javascript" src="min/myjs.js"></script>

i tried the same , using the <?php bloginfo('template_directory'); ?>

<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/min/myjs.js"></script>

Help?

Share Improve this question edited Jul 31, 2013 at 10:27 Jeremi Liwanag asked Jul 31, 2013 at 10:01 Jeremi LiwanagJeremi Liwanag 9644 gold badges22 silver badges45 bronze badges 9
  • you checked where is the actual path of your js and css file – Sonu Sindhu Commented Jul 31, 2013 at 10:09
  • What errors does it cause? – Tdelang Commented Jul 31, 2013 at 10:16
  • the Path is correct, because its working in the HTML version. Errors i see is in the dreamweaver.. it goes red. Means not good right?? – Jeremi Liwanag Commented Jul 31, 2013 at 10:21
  • The way you're closing your php tags looks odd to me. What happens if you remove the space between the ? and > (so ?>)? – Hobo Commented Jul 31, 2013 at 10:24
  • removed space. still doesnt work.. :-( – Jeremi Liwanag Commented Jul 31, 2013 at 10:26
 |  Show 4 more ments

2 Answers 2

Reset to default 5

Don't include scripts and stylesheets this way. Use wp_enqueue_scripts. Assuming this is a custom theme, add the code to the functions.php file.

add_action( 'wp_enqueue_scripts', 'theme_scripts_styles' );

function theme_scripts_styles() {

  // Enqueue scripts and styles here

}

In this function use wp_enqueue_script and wp_enqueue_style to queue the files (should be obvious which to use based on the file type!)

Also bloginfo( 'template_directory' ) is not best practice.

Use get_stylesheet_directory_uri(), or get_template_directory_uri() for parent themes. You can drop the _uri part to get the server paths.

So all together it looks something like

add_action( 'wp_enqueue_scripts', 'theme_scripts_styles' );

function theme_scripts_styles() {

  wp_enqueue_style( 'my-styles', get_stylesheet_directory_uri() . '/min/mycss.css', array(), '1.0', 'all' );
  wp_enqueue_script( 'my-scripts', get_stylesheet_directory_uri() . '/min/myjs.js', array(), '1.0', true );

}

Be sure to read the Codex on the enqueue functions so you know what all the parameters do.

Please use this code to add your JavaScript link to your wordpress site.

<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/path_to/your_script.js"></script>
发布评论

评论列表(0)

  1. 暂无评论