我想将 JQuery 脚本添加到单个 Wordpress 页面中,但我不知道如何操作。我想注入(inject)的脚本示例:
<script type="text/javascript">
$(document).ready(function() {
$("html, body").animate({ scrollTop: $(window).height() }, 600);
return false;
});
</script>
这段代码在纯 HTML 中注入(inject)时工作正常,但我如何为 Wordpress 做同样的事情?
最佳答案
我个人认为将 JavaScript 添加到特定页面/帖子的最佳方法是使用 ShortCode
添加此:
function add_my_script() {
return "<script>
//your jQuery here
</script>";
}
add_shortcode( 'myCustomShortCode', 'add_my_script' );
给您的function.php
文件。您的 function.php 文件位于 /wp-content/themes/<name of theme>/
注意:使用'
而不是"
在你的<script>
继续进入 return
陈述。
现在只需添加短代码 [myCustomShortCode]
在您的页面中。
关于wordpress - 如何将 JQuery 脚本添加到单个 WordPress 页面中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24969155/