我想知道最有效的方法是为帖子和/或页面添加专门的javascript文件。
以下是我提出的一些解决方案:
切换到HTML编辑视图并在其中发布您的JavaScript(非常糟糕的解决方案)
有什么想法吗?
我想知道最有效的方法是为帖子和/或页面添加专门的javascript文件。
以下是我提出的一些解决方案:
切换到HTML编辑视图并在其中发布您的JavaScript(非常糟糕的解决方案)
有什么想法吗?
我认为效率和使用适当的wordpress方法添加javascript之间的最佳平衡是在主题函数中添加类似的内容。php文件。例如:
functions.php:
function load_scripts() {
global $post;
if( is_page() || is_single() )
{
switch($post->post_name) // post_name is the post slug which is more consistent for matching to here
{
case \'home\':
wp_enqueue_script(\'home\', get_template_directory_uri() . \'/js/home.js\', array(\'jquery\'), \'\', false);
break;
case \'about-page\':
wp_enqueue_script(\'about\', get_template_directory_uri() . \'/js/about-page.js\', array(\'jquery\'), \'\', true);
break;
case \'some-post\':
wp_enqueue_script(\'somepost\', get_template_directory_uri() . \'/js/somepost.js\', array(\'jquery\'), \'1.6\', true);
break;
}
}
}
add_action(\'wp_enqueue_scripts\', \'load_scripts\');
这使您能够完全控制加载内容的位置,即主题功能中的集中位置。用于编辑加载内容的php文件:并且,这种方式使用wordpress方法将javascript添加到您的帖子和页面中safely.我要做的是在页脚或页眉中放置并使用php条件。
例如:
<?php if (is_page (\'your-page\')){?>
<script type="text/javascript" src"the file path"></script>
<?php } elseif ( is_page (\'another\')){?>
<script type="text/javascript" src"the file path"></script>
<?php } else { ?>
<script type="text/javascript" src"the file path"></script>
<?php } ?>
这样,您就不会在每次页面加载时一直调用所有脚本,而只调用所需的脚本。这里是Wordpress codex的链接http://codex.wordpress.org/Conditional_Tags
如果您有可能只需要按帖子调用的特殊脚本,请使用自定义字段。
另一种测试方法是直接从编辑器的加载项页面下方添加注释,并使用脚本中的标签添加注释,否则将不起作用。
<script type="text/javascript">
<!--
var a = 5;
alert("hello world. The value of a is: " + a);
-->
</script>
将单个脚本应用于特定页面的最佳实践是什么?当我尝试应用整个页面的脚本时,它与其他插件有问题。有经验法则吗? <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js\"></script> <script> (function() { $(\'html\').addClass(\'js\'); &#x