简体   繁体   English

javascript应该放在Wordpress中,这只是single.php所必需的?

[英]Where should javascript go in Wordpress which is only necessary for single.php?

I want to add social buttons (Facebook, Twitter, G+) at the end of a post in a Wordpress site I'm building. 我想在我正在构建的Wordpress网站的帖子末尾添加社交按钮(Facebook,Twitter,G +)。 They only need to be shown on a post's permalink page (which is edited in single.php). 它们只需要显示在帖子的永久链接页面上(在single.php中编辑)。

So, my question is, should I put the necessary scripts in header.php (which would put them in place but they would always load even when not necessary), or should I put them somewhere in single.php (which would mean they only load when necessary but are located somewhere in the middle of <body> )? 所以,我的问题是,我是否应该在header.php中放置必要的脚本(这会将它们放在适当的位置,但即使在没有必要时它们也会一直加载),或者我应该将它们放在single.php中的某个地方(这意味着它们只是必要时加载,但是位于<body>中间的某个位置?

WordPress has a function called is_single() which returns if you are dealing with a single page at the moment. WordPress有一个名为is_single()函数 ,如果你正在处理单个页面,它会返回。

You could include the scripts only if a single page is viewed. 只有在查看单个页面时才可以包含脚本。 You could do it with 你可以做到

<?php if (is_single()) : ?>
  <script src="http://twitter.com/path/to/bla"></script>
<?php endif; ?>

You may want to load scripts at the end of the page (right before the closing body -tag) as it lets the page render before loading the scripts. 您可能希望在页面末尾(在关闭body -tag之前)加载脚本,因为它允许在加载脚本之前呈现页面。

The best way to do this is to use the wp_enqueue_scripts action hook. 执行此操作的最佳方法是使用wp_enqueue_scripts操作挂钩。 You should always use this to avoid conflicts and keep all the goodness that comes with it. 你应该总是使用它来避免冲突并保持随之而来的所有好处。

I slightly modified an example from the doc to illustrate this: 我稍微修改了一个来自doc示例来说明这一点:

<?php
 function my_scripts_method() {
 if(is_single()){
  // register your script location, dependencies and version
  wp_register_script('custom_script',
    get_template_directory_uri() . '/js/custom_script.js',
    array('jquery'),
    '1.0' );
  // enqueue the script
  wp_enqueue_script('custom_script');
 }
 add_action('wp_enqueue_scripts', 'my_scripts_method');
?>

There's nothing wrong with adding script elements mixed in the middle of the body , however if you want to improve loading times, you might want to append the script elements to footer.php . 添加在body中间混合的脚本元素没有任何问题,但是如果要增加加载时间,可能需要将script元素附加到footer.php Having scripts called after the rest of the dom has loaded will prevent the loading of the scripts from blocking the rest of the page from rendering. 在加载dom的其余部分之后调用脚本将阻止加载脚本阻止页面的其余部分呈现。

If it's a very small script, I see no reason not to just add them inline where it'll be easy to see what elements they affect. 如果它是一个非常小的脚本,我认为没有理由不将它们添加到内联中,这样可以很容易地看到它们影响的元素。

You could put them at the bottom of your single.php, but as you point out this would leave them appearing mid-page. 你可以将它们放在single.php的底部,但正如你所指出的那样,这会让它们出现在页面中间。

Instead, put them in your header.php, but insider an if statement to check that they are needed (ie it is a single page), like so: 相反,将它们放在header.php中,但内部使用if语句来检查它们是否需要(即它是单页),如下所示:

<?php if (is_single()): ?>
     <script>...</script>
<?php endif;?>

The first <?php and last ?> wil not be necessary / may cause problems if you put the above code insider php tags. 如果你把上面的代码放在内部的php标签中,第一个<?php和last ?>不会/可能会导致问题。 Either way, inside the if statement, should be HTML, not php for this to work. 无论哪种方式,在if语句中,应该是HTML,而不是php才能使用。

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

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