dousi0144
2012-06-05 02:52
浏览 63
已采纳

我对wp_enqueue_script的新方法

OK, i'm trying to eliminate all possible variables for troubleshooting purposes. So instead of in my functions.php, I've dropped this code in my header.php file:

<?php $mdgtemplateloc = get_bloginfo( 'template_url' ).'/js/'; ?>
<?php echo '<!-- ' . $mdgtemplateloc . ' --> ?>
<?php wp_enqueue_script( 'hoverIntent', $mdgtemplateloc.'hoverIntent.js', array( 'jquery' ) ); ?>
<?php wp_enqueue_script( 'mdMenuAnimation', $mdgtemplateloc.'mdMenuAnimation.js', array( 'hoverIntent' ) ); ?>

The result is a few white space insertions into the source and the comment appears as requested. My understanding was that this would insert something like

<script type="text/javascript" src="[url]/js/mdMenuAnimation.js"></script>

I want to do this the correct way, but wp_enqueue_script has been giving me NOTHING. I suspect I'm doing something fundamentally wrong, but I can't find it and nothing I find through google or stackoverflow or the wp codex is helping at all.

To clarify, here's what I had before, in the functions.php file:

function mdg_setup_scripts() {
  $mdgtemplateloc = get_bloginfo( 'template_url' ).'/js/';
  wp_register_script( 'hoverIntent', get_bloginfo('template_url').'/js/hoverIntent.js', array( 'jquery' ));
  wp_enqueue_script( 'hoverIntent' );
  wp_register_script( 'mdMenuAnimation', $mdgtemplateloc.'mdMenuAnimation.js', array( 'hoverIntent' ));
  wp_enqueue_script( 'mdMenuAnimation' );
}
add_action( 'wp_enqueue_scripts', 'mdg_setup_scripts' );

This also produced no output that ever called a script. I understand that this second is more like what is supposed to be, but it isn't doing anything.

图片转代码服务由CSDN问答提供 功能建议

好的,我正在尝试消除所有可能的变量以进行故障排除。 所以我没有在我的functions.php中删除这个代码,而是将我的代码放在我的header.php文件中:

 &lt;?php $ mdgtemplateloc = get_bloginfo('template_url')。  '/ JS /';  ?&gt; 
&lt;?php echo'&lt;! - '。  $ mdgtemplateloc。  ' - &gt;  ?&gt; 
&lt;?php wp_enqueue_script('hoverIntent',$ mdgtemplateloc.'hoverIntent.js',array('jquery'));  ?&gt; 
&lt;?php wp_enqueue_script('mdMenuAnimation',$ mdgtemplateloc.'mdMenuAnimation.js',array('hoverIntent'));  ?&gt; 
   
 
 

结果是在源中插入了一些空格,注释按请求显示。 我的理解是这会插入像

 &lt; script type =“text / javascript”src =“[url] /js/mdMenuAnimation.js”&gt;&lt;  / script&gt; 
   
 
 

我想以正确的方式做到这一点,但是wp_enqueue_script一直在给我什么。 我怀疑我做的事情从根本上是错误的,但我找不到它,我通过google或stackoverflow找不到任何东西,或者wp codex都没有帮助。

澄清一下,这是什么 我之前在functions.php文件中:

  function mdg_setup_scripts(){
 $ mdgtemplateloc = get_bloginfo('template_url')。'/ js /'; 
  wp_register_script('hoverIntent',get_bloginfo('template_url')。'/ js / hoverIntent.js',array('jquery')); 
 wp_enqueue_script('hoverIntent'); 
 wp_register_script('mdMenuAnimation',$ mdgtemplateloc。  'mdMenuAnimation.js',array('hoverIntent')); 
 wp_enqueue_script('mdMenuAnimation'); 
} 
add_action('wp_enqueue_scripts','mdg_setup_scripts'); 
   
  
 

这也没有产生任何曾经调用过脚本的输出。 我知道这第二个更像是应该是什么,但它没有做任何事情。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douqi1212 2012-06-05 03:00
    最佳回答

    wp_enqueue_script() does not output anything. You're using it incorrectly.

    You need to create a callback and add it to the wp_enqueue_scripts action. Typically this belongs in your functions.php file.

    Re-read the docs. There are examples of exactly what you are wanting to do.

    评论
    解决 无用
    打赏 举报